오늘은 메인 프로그램에 직접 만든 xaml 파일을 적용하는 방법을 정리하였습니다.
ResourceDictionary 사용
📌직접 생성
[프로젝트 -> 새 항목] 에서 리소스 사전을 선택하고 xaml 파일을 생성한다. (전 MS의 ExpressionDark.xaml로 했습니다.)
그러면 아래와 같은 Xaml이 만들어 집니다.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>
📌파일 가져오기
ExpressionDark.xaml는 MS에 지원하는 WPF 테마 입니다.
https://github.com/StanislawSwierc/WpfThemesCollection/blob/master/Themes/ExpressionDark.xaml
GitHub - StanislawSwierc/WpfThemesCollection: Collection of WPF Themes
Collection of WPF Themes. Contribute to StanislawSwierc/WpfThemesCollection development by creating an account on GitHub.
github.com
App.xaml에 적용
메인 프로그램에 생성한 ExpressionDark.xaml을 적용하고 싶을 때는 아래와 같이 하면 됩니다.
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="ExpressionDark.xaml"></ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml에 적용
다음과 같이 버튼 한개를 생성합니다.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Click="Button_Click" Margin="10">버튼</Button>
</Grid>
</Window>
실행된 프로그램을 보면 기본 버튼이 아닌 ExpressionDark 테마가 적용된 것을 볼 수 있습니다.
버튼에 어떻게 테마가 적용되었는지를 알기 위해서는 Style, ControlTemplate 태그를 알아야 합니다.
다음 포스팅에서 정리해보겠습니다.
감사합니다.
'WPF' 카테고리의 다른 글
[WPF] - INotifyPropertyChanged를 이용한 Binding 방법 (0) | 2023.01.01 |
---|---|
[WPF] - Border (0) | 2022.12.31 |
[WPF] - Style, ControlTemplate (0) | 2022.12.31 |
[WPF] - 프로젝트 생성 및 시작 (0) | 2022.12.28 |
[WPF] - 시작 (0) | 2022.12.28 |