다른 Resource Dictionary 안에 존재하는 다른 Stsyle을 c# 코드로 변경하는 방법입니다.
|
string resourcePath = "/프로젝트이름;component/ResourceDictionary위치.xaml"; Control.Style = (Style)res["ResourceKey"]; |

예제를 따라 하시려면 더보기를 클릭하세요.
Grid 안에 Label과 Button을 한 개씩 준비합니다.

| xaml |
| <Grid x:Name="G1"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <Label Name="L1"/> <Button x:Name="B1" Click="Button_Click"/> </StackPanel> </Grid> |
2가지 Resource Dictionary를 준비합니다.
| ResourceDictionary : LightMode.xaml |
| <Style TargetType="Grid" x:Key="GridStyle"> <Setter Property="Background" Value="GhostWhite"/> </Style> <Style TargetType="Label" x:Key="LabelStyle"> <Setter Property="Content" Value="라이트 모드"/> <Setter Property="FontSize" Value="20"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Foreground" Value="Black"/> </Style> <Style TargetType="Button" x:Key="ButtonStyle"> <Setter Property="Width" Value="50"/> <Setter Property="Height" Value="30"/> <Setter Property="Content" Value="변경"/> <Setter Property="BorderBrush" Value="Black"/> <Setter Property="Background" Value="WhiteSmoke"/> </Style> |
| ResourceDictionary : DarkMode.xaml |
| <Style TargetType="Grid" x:Key="GridStyle"> <Setter Property="Background" Value="Black"/> </Style> <Style TargetType="Label" x:Key="LabelStyle"> <Setter Property="Content" Value="다크 모드"/> <Setter Property="FontSize" Value="20"/> <Setter Property="FontWeight" Value="ExtraBold"/> <Setter Property="Foreground" Value="White"/> </Style> <Style TargetType="Button" x:Key="ButtonStyle"> <Setter Property="Width" Value="50"/> <Setter Property="Height" Value="30"/> <Setter Property="Content" Value="변경"/> <Setter Property="Foreground" Value="White"/> <Setter Property="BorderBrush" Value="White"/> <Setter Property="Background" Value="Black"/> </Style> |
| c# | |
|
위와 같은 방법은 컴포넌트 1개의 스타일을 바꿀 때는 유용하지만 라이트 모드/다크 모드처럼 모든 것을 변경할 때는 귀찮습니다. 이럴 때 아래와 같이 사용하는 것이 편리합니다.
| App.xaml |
| <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary x:Name="ThemeDictionary"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/WpfApp1;component/LightMode.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> |
| App.xaml.cs | |
|
| xml |
| <Grid x:Name="G1" Style="{DynamicResource GridStyle}"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <Label Name="L1" Style="{DynamicResource LabelStyle}"/> <Button x:Name="B1" Style="{DynamicResource ButtonStyle}" Click="Button_Click"/> </StackPanel> </Grid> |
| c# | |
|
참고 자료
stackoverflow.com/questions/33867091/how-to-change-resourcedictionary-dynamically
How to change ResourceDictionary Dynamically
I need to change ResourceDictionary in App.xaml file dynamically. I have tried the following code: ResourceDictionary newRes = new ResourceDictionary(); newRes.Source = new Uri("/PsyboInventory;
stackoverflow.com
'프로그램 개발 > C#' 카테고리의 다른 글
| [wpf] c# 코드로 폴더 선택하기 - FolderBroswerDialog와CommonOpenDialog (0) | 2021.05.02 |
|---|---|
| [wpf] c# 코드로 작업 끝내기 (0) | 2021.04.14 |
| [wpf] c# 코드로 Style 설정하기 - 1 : 같은 Resource Dictionary 사용 (0) | 2021.01.17 |
| [wpf] c# 코드로 Label 색 변경하기 (0) | 2021.01.10 |
| [wpf] Button 안의 Button 클릭 이벤트 (0) | 2021.01.03 |