样式WPF - 代码中的Material Design GroupBox

时间:2018-04-18 15:51:10

标签: c# wpf xaml groupbox material-design-in-xaml

经过相当多的搜索/反复试验后,我必须求助于社区对此的帮助。我正在使用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。

非常感谢任何帮助!

提前致谢

2 个答案:

答案 0 :(得分:3)

为了扩展Mikael的答案,当你不使用Material Design时这很好用,我能够解决这个问题:

childDef.Style = Application.Current.FindResource("MaterialDesignGroupBox") as Style;

我仍然在寻找一种方法来应用MaterialDesign:ColorZoneAssist.Mode =&#34; Accent&#34;在代码背后,似乎这是应用除应用程序的主要MaterialDesign颜色之外的其他颜色的唯一方法。

答案 1 :(得分:2)

您可以使用后面代码中的Application.Current.Resources

childDef.BorderBrush = (Brush) Application.Current.Resources["SecondaryAccentBrush"];

例如,访问&#34; SecondaryAccentBrush&#34;在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);

具有以下结果:

Brush from code behind