【问题标题】:Styling WPF - Material Design GroupBox in Code样式化 WPF - 代码中的材料设计 GroupBox
【发布时间】: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


    【解决方案1】:

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

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

    我仍在寻找一种在后面的代码中应用 MaterialDesign:ColorZoneAssist.Mode="Accent" 的方法,因为这似乎是应用不同于应用程序主要 MaterialDesign 颜色的唯一方法.

    【讨论】:

      【解决方案2】:

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

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

      例如访问 App.xaml 中定义的“SecondaryAccentBrush”画笔:

      <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,虽然它非常有用并且让我看到了一些我不知道的东西,但它不适用于我的情况。似乎材料设计完全忽略了新的画笔定义。
      • MIkael,我赞成您的回答,因为它对我有用。我是 StackOverflow 的新手,所以我的投票还没有必要的声誉来改变公开显示的内容。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多