问题描述
我正在使用Material Design设置WPF应用的样式。我已经使用以下代码来设置标签视图的样式:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Material Design -->
<ResourceDictionary Source="pack://application:,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
<ResourceDictionary Source="pack://application:,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
<!-- Styles -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--<ResourceDictionary Source="pack://application:,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />-->
</ResourceDictionary.MergedDictionaries>
<!-- Tab Controller Primary Styles -->
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="#212121"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="#212121"/>
<!-- This causes a clash between the tabview and the textBox colors -->
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#212121"/>
<!--<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#BB86FC"/>-->
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="#FFFFFF"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="#212121"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="#212121"/>
</ResourceDictionary>
<!-- Tab Controller Secondary Styles -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="SecondaryAccentBrush" Color="#BB86FC"/>
<SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="#BB86FC"/>
</ResourceDictionary>
<ResourceDictionary Source="pack://application:,/Dragablz;component/Themes/materialdesign.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style targettype="{x:Type dragablz:TabablzControl}" BasedOn="{StaticResource MaterialDesignTabablzControlStyle}" />
</ResourceDictionary>
我的问题是设置TabController主要样式的样式,它会更改应用程序中TextBox的颜色。
当我将深色应用于选项卡视图的样式时,TextBox将失去其紫色突出显示。
但是现在在第二张图像中,当我将TextBox的颜色更改回紫色时,TabView看起来很恐怖。
它们的样式都依赖于相同的字典键。有什么方法可以更改x:Key值,具体取决于它应用于的对象类型?保留两个组件所需样式的最佳方法是什么。
这是给我的大学论文的,所以我感谢您的帮助!
祝一切顺利。
解决方法
使用string```
,可以在各种样式的控件中使用Material Design笔刷。这意味着,如果在运行时使用相同的DynamicResource
替换或注册画笔,则将修改所有动态引用,并且所做的更改将应用于定义了它的资源字典范围内的所有控件。
通常,您必须复制控件的默认样式和模板,以便将更改后的颜色应用于该控件并进行调整,例如通过创建单独的画笔并使用它代替原始画笔,可以直接在控件模板中分配值,而不是模板绑定。
如果使用x:Key
,可能会有更简单的方法。从其default style at GitHub中,我只能看到TabablzControl
中设置为PrimaryHueMidBrush
的{{1}}的一种用法。您可以按照自己的风格进行书写。
BorderBrush
当然,您可以创建一个自定义画笔并通过Setter
对其进行引用,而不是对其进行硬编码。
<Style TargetType="{x:Type dragablz:TabablzControl}" BasedOn="{StaticResource MaterialDesignTabablzControlStyle}">
<Setter Property="BorderBrush" Value="#212121"/>
</Style>