【发布时间】:2018-04-18 15:51:10
【问题描述】:
经过相当多的搜索/反复试验,我必须求助于社区对此的帮助。我正在使用 MaterialDesign 框架开发一个 WPF 应用程序,该框架在我的 App.xaml 中定义了自定义颜色,如下所示:
<Application.Resources>
<ResourceDictionary>
<!-- primary -->
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="#033059"/><!--5c5b5e-->
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="#FFFFFF"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="DarkRed"/> <!--6134d9-->
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="#FFFFFF"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="#48545e"/> <!--4D1DCF-->
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="#FFFFFF"/>
<!-- accent -->
<SolidColorBrush x:Key="SecondaryAccentBrush" Color="#1266a7"/> <!--5c5b5e-->
<SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="#FFFFFF"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<!--<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Red.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Blue.xaml" />-->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Button.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.GroupBox.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.DatePicker.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Calendar.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.CheckBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
需要以编程方式(在代码中)呈现某些控件,其中一个控件是 GroupBox。这个 GroupBox 渲染得很好,但使用 PrimaryHueMidBrush 中定义的颜色设置样式。我想要完成的是为 GroupBox 分配另一个(SecondaryAccentBrush)。在 XAML 中,这可以实现如下:
<Border BorderBrush="{DynamicResource SecondaryAccentBrush}" BorderThickness="2" Padding="10" Height="50" Width="300">
<TextBlock Text="Material Design Test" />
</Border>
var childDef = new GroupBox()
{
Header = "Deficiencies",
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Top,
//Background = new SolidColorBrush(Colors.Red)
//Foreground = new SolidColorBrush(Colors.Red),
//OverridesDefaultStyle = true
//Background = Brushes.DarkRed,
//BorderThickness = new Thickness(1),
//BorderBrush = new SolidColorBrush(Colors.Red)
};
//childDef.SetResourceReference(Control.BorderBrushProperty, "PrimaryHueDarkBrush");
//childDef.SetResourceReference(Control., materialDesign:ColorZoneAssist.Mode="Accent");
问题是,我不知道如何通过代码实现这一点。我尝试的任何方法似乎都不起作用。我不知道如何覆盖 BorderBrushProperty。
任何帮助将不胜感激!
提前致谢
【问题讨论】:
标签: c# wpf xaml groupbox material-design-in-xaml