我想要完成的是允许我的用户通过从列表中选择主颜色来更改我的应用程序的整体颜色主题。我已经完成了这项工作,但如果我能弄清楚如何去做,我注意到有一些事情可能会让我的生活更轻松。现在我有一个看起来像这样的主题文件夹。包含其他文件夹和通用资源字典的主题文件夹(即用于背景的画笔)。下一组文件夹是 RedTheme、BlueTheme 文件夹,每个文件夹都包含用于各个控件的资源字典;例如,BlueTheme 有一个用于按钮的资源字典,RedTheme 也有。我注意到的有趣的事情是资源字典几乎相同,除了只有一个 SolidColorBrush 的颜色。所以我' 我试图以某种方式绑定那个画笔,这样我就不会在相同资源字典的副本上复制只有一个区别。我会尽我所能输入我的代码,但它的性质已经足够复杂,实际上无法查看不同的字典。另外我应该补充一点,我在嵌套字典中嵌套字典。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>
这是我希望发生动态更改的地方 我只想在选择新主题时将画笔绑定在代码隐藏的某个位置,然后将该画笔弹出到我试图突出显示的位置,这样我只需要生成这些字典之一,而不是为每种颜色生成一个。
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;
}
}
}