私が達成したいのは、リストからマスターカラーを選択することで、ユーザーがアプリケーションの全体的なカラーテーマを変更できるようにすることです。私はこれを機能させましたが、それを行う方法を理解できれば、私の人生を楽にする可能性があることに気づいたことがありました。現在、このようなテーマフォルダがあります。全体的に使用される他のフォルダーと一般的なリソース辞書(つまり、背景に使用されるブラシ)を保持するテーマフォルダー。次のフォルダセットはRedTheme、BlueThemeフォルダで、それぞれに個々のコントロールのリソースディクショナリが含まれています。たとえば、BlueThemeにはボタンのリソース辞書があり、RedThemeにもあります。私が気付いた興味深い点は、リソースディクショナリが1つのSolidColorBrushの色を除いてほとんど同じであるということです。だから私は mどういうわけかそのブラシをバインドしようとしているので、同じリソースディクショナリのコピーに1つの違いだけでコピーができてしまうことはありません。私は自分のコードを入れるために最善を尽くしますが、実際にさまざまな辞書を見ることができずに、その性質は十分に複雑です。また、ネストされた辞書内に辞書をネストしていることを追加する必要があります。App.xamlには、複数の辞書自体を含むすべての辞書が含まれており、テーマの変更といくつかの一般的な辞書のためにスワップインまたはスワップアウトされます。また、ネストされた辞書内に辞書をネストしていることを追加する必要があります。App.xamlには、複数の辞書自体を含むすべての辞書が含まれており、テーマの変更といくつかの一般的な辞書のためにスワップインまたはスワップアウトされます。また、ネストされた辞書内に辞書をネストしていることを追加する必要があります。App.xamlには、複数の辞書自体を含むすべての辞書が含まれており、テーマの変更といくつかの一般的な辞書のためにスワップインまたはスワップアウトされます。
ThemeBlue.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BlueTheme/BlueBrushes.xaml"/>
<ResourceDictionary Source="BlueTheme/ButtonStyleAndTemplate(normal_blue).xaml"/>
<ResourceDictionary Source="BlueTheme/LabelStyleAndTemplate(normal_blue).xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
ThemeRed.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="RedTheme/RedBrushes.xaml"/>
<ResourceDictionary Source="RedTheme/ButtonStyleAndTemplate(normal_red).xaml"/>
<ResourceDictionary Source="RedTheme/LabelStyleAndTemplate(normal_red).xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
これは、動的な変更 を実行したい場所です。新しいテーマが選択されたときに、コードビハインドのどこかにブラシをバインドし、そのブラシをハイライトしようとしたスポットにポップするだけです。色ごとに1つではなく、これらの辞書の1つを生成します。
Style TargetType="{x:Type mycon:MyButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type mycon:MyButton}">
<Grid Margin="2">
<Ellipse Name="MainCircle" Fill="{DynamicResource ResourceKey=TranslucentBrush}" Stroke="{DynamicResource ResourceKey=***RedBorderBrush***}" />
<Ellipse Name="RefractionCircle" Fill="{DynamicResource ResourceKey=ButtonRefractionLayer}"/>
<mycon:ButtonTextBlock Margin="0,0,0,8" FontWeight="Black" FontSize="20" Foreground="{DynamicResource ResourceKey=***RedBorderBrush***}" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Path x:Name="ReflectionLayer" VerticalAlignment="Top" Stretch="Fill">
<Path.RenderTransform>
<ScaleTransform ScaleY="0.5" />
</Path.RenderTransform>
<Path.Data>
<PathGeometry>
<PathFigure IsClosed="True" StartPoint="98.999,45.499">
<BezierSegment Point1="98.999,54.170" Point2="89.046,52.258" Point3="85.502,51.029"/>
<BezierSegment IsSmoothJoin="True" Point1="75.860,47.685" Point2="69.111,45.196" Point3="50.167,45.196"/>
<BezierSegment Point1="30.805,45.196" Point2="20.173,47.741" Point3="10.665,51.363"/>
<BezierSegment IsSmoothJoin="True" Point1="7.469,52.580" Point2="1.000,53.252" Point3="1.000,44.999"/>
<BezierSegment Point1="1.000,39.510" Point2="0.884,39.227" Point3="2.519,34.286"/>
<BezierSegment IsSmoothJoin="True" Point1="9.106,14.370" Point2="27.875,0" Point3="50,0"/>
<BezierSegment Point1="72.198,0" Point2="91.018,14.466" Point3="97.546,34.485"/>
<BezierSegment IsSmoothJoin="True" Point1="99.139,39.369" Point2="98.999,40.084" Point3="98.999,45.499"/>
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.Fill>
<RadialGradientBrush GradientOrigin="0.498,0.526">
<RadialGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1" ScaleY="1.997"/>
<TranslateTransform X="0" Y="0.5"/>
</TransformGroup>
</RadialGradientBrush.RelativeTransform>
<GradientStop Offset="1" Color="#99FFFFFF"/>
<GradientStop Offset="0.85" Color="#72FFFFFF"/>
<GradientStop Offset="0" Color="#00000000"/>
</RadialGradientBrush>
</Path.Fill>
</Path>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="MainCircle" Property="Fill" Value="{DynamicResource ResourceKey=TransparentBrush}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="MainCircle" Property="Fill" Value="{DynamicResource ResourceKey=HighlitTranslucentBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- EndRegion -->
</ResourceDictionary>
-------メインウィンドウの背後にあるコード----------
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SetDefaultStyle();
populateCboTheme();
}
private void populateCboTheme()
{
cboTheme.Items.Add("Red");
cboTheme.Items.Add("Blue");
cboTheme.Items.Add("Green");
cboTheme.Items.Add("Yellow");
}
private void SetDefaultStyle()
{
SetNewStyle("Red");
}
private void SetNewStyle(string _color)
{
Uri themeUri = new Uri(string.Format("Themes/Theme{0}.xaml",_color),UriKind.Relative);
ResourceDictionary theme = (ResourceDictionary)Application.LoadComponent(themeUri);
Resources.MergedDictionaries.Add(theme);
}
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void cboTheme_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (cboTheme.SelectedIndex)
{
case 0:
SetNewStyle("Red");
break;
case 1:
SetNewStyle("Blue");
break;
case 2:
SetNewStyle("Green");
break;
case 3:
SetNewStyle("Yellow");
break;
}
}
}