使用嵌套资源字典动态更改wpf中的主题

时间:2013-03-08 05:08:47

标签: c# wpf data-binding themes resourcedictionary

我想要实现的是允许我的用户通过从列表中选择主颜色来更改应用程序的整体颜色主题。我有这个工作,但有一些我注意到,如果我能弄清楚如何做,可能会让我的生活更轻松。现在我有一个看起来像这样的Theme文件夹。主题文件夹,包含整个板上使用的其他文件夹和通用资源词典(即用于背景的画笔)。下一组文件夹是RedTheme,BlueTheme文件夹,每个文件夹都包含各个控件的资源字典;例如,BlueTheme有一个按钮的资源字典,RedTheme也是如此。我注意到的有趣的事情是资源字典几乎完全相同,只有一个SolidColorBrush的颜色除外。所以我试图以某种方式绑定那个画笔,这样我就不会在同一个资源字典的副本上只复制一个副本。我将尽我所能放入我的代码,但它的本质很复杂,实际上却无法看到不同的字典。另外我应该补充一点,我在嵌套字典中嵌套字典。 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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

好的,所以DHN是对的...我展示了很多代码,但没有给出很多我正在寻找的解释。自从这篇文章以来,我偶然发现了我正在寻找的答案。基本上不是将画笔和控件一起包装在一个资源字典中并将其交换进去,而是让控制字典指向一个通用的命名键画笔,并为每种颜色制作不同的画笔字典。即使这个解决方案仍然存在重复代码的问题......它比以前更少重复FAR。

<solidcolorbrush x:key="RedBorderBrush".../>
现在是:<solidcolorbrush x:key="BorderBrush".../>
对于所有不同的颜色都是一样的 然后我指出<... Fill="{dynamicresource resourcekey="BorderBrush"}" 然后,当我交换颜色时(因为所有画笔字典都命名为边框画笔&#34; BorderBrush&#34;)颜色会发生变化。

相关问题