样式化 WPF - Material 在代码中设计 GroupBox
Styling WPF - Material Design GroupBox in Code
经过大量搜索/反复试验后,我必须求助于社区对此问题的帮助。我正在使用 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 中定义的颜色设置样式。我想要完成的是将另一个 (SecondaryAccentBrush) 分配给 GroupBox。在 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。
如有任何帮助,我们将不胜感激!
提前致谢
您可以在后面的代码中使用 Application.Current.Resources:
childDef.BorderBrush = (Brush) Application.Current.Resources["SecondaryAccentBrush"];
因此,例如访问 "SecondaryAccentBrush" 在 App.xaml 中定义的画笔:
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="SecondaryAccentBrush" Color="#1266a7"/>
</ResourceDictionary>
</Application.Resources>
您可以执行以下操作:
var grp = new GroupBox
{
BorderBrush = (Brush) Application.Current.Resources["SecondaryAccentBrush"],
Header = "test",
Width = 100,
Height = 100,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
this.MyGrid.Children.Add(grp);
结果如下:
为了扩展 Mikael 的答案,当你不使用 Material 设计时它工作得很好,我能够解决这个问题:
childDef.Style = Application.Current.FindResource("MaterialDesignGroupBox") as Style;
我仍在寻找在代码隐藏中应用 MaterialDesign:ColorZoneAssist.Mode="Accent" 的方法,因为这似乎是除了主要Material应用程序的设计颜色。
经过大量搜索/反复试验后,我必须求助于社区对此问题的帮助。我正在使用 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 中定义的颜色设置样式。我想要完成的是将另一个 (SecondaryAccentBrush) 分配给 GroupBox。在 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。
如有任何帮助,我们将不胜感激!
提前致谢
您可以在后面的代码中使用 Application.Current.Resources:
childDef.BorderBrush = (Brush) Application.Current.Resources["SecondaryAccentBrush"];
因此,例如访问 "SecondaryAccentBrush" 在 App.xaml 中定义的画笔:
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="SecondaryAccentBrush" Color="#1266a7"/>
</ResourceDictionary>
</Application.Resources>
您可以执行以下操作:
var grp = new GroupBox
{
BorderBrush = (Brush) Application.Current.Resources["SecondaryAccentBrush"],
Header = "test",
Width = 100,
Height = 100,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
this.MyGrid.Children.Add(grp);
结果如下:
为了扩展 Mikael 的答案,当你不使用 Material 设计时它工作得很好,我能够解决这个问题:
childDef.Style = Application.Current.FindResource("MaterialDesignGroupBox") as Style;
我仍在寻找在代码隐藏中应用 MaterialDesign:ColorZoneAssist.Mode="Accent" 的方法,因为这似乎是除了主要Material应用程序的设计颜色。