【问题标题】:Overriding WPF UserControl default theme覆盖 WPF UserControl 默认主题
【发布时间】:2013-08-21 17:19:30
【问题描述】:

我在 WPF 项目中使用MahApps.Metro。但是我没有使用 nuget 包,而是下载了源代码并将其作为项目添加到我的解决方案中(出于某些原因)。如您所知,它的所有控件都有一个默认主题。现在,我正在尝试在我的项目中更改 MetroWindow 的默认值。我不想改变原来的项目。所以我创建了一个新的ResourceDictionary 并且我正在设计它。我试图更改MetroWindow 默认它们,但似乎没有x:Key 属性是不可能的。我的意思是我试过这个:

<Style TargetType="{x:Type metro:MetroWindow}">
    <!-- My styles and templates gone here... -->
</Style>

还有这个:

<Style x:Key="{x:Type metro:MetroWindow}" TargetType="{x:Type metro:MetroWindow}">
    <!-- My styles and templates gone here... -->
</Style>

但是它们都没有对控件产生影响,并且控件仍然使用MahApps.Metro定义的默认主题。我错过了什么?任何的想法?甚至可能吗?

【问题讨论】:

    标签: wpf themes resourcedictionary wpf-4.0 mahapps.metro


    【解决方案1】:

    我也尝试过相同的方法,但事实证明无法以这种方式更改窗口样式。唯一的方法是通过rolling your own window

    至于控件,你可以使用eg来改变样式:

    <Style TargetType="Button" BasedOn="{StaticResource MetroButton}">
        <!-- your changes here -->
    </Style>
    

    【讨论】:

      【解决方案2】:

      另一种方法是创建自己的主题。

      1. 从 Mahapps 复制 BaseLight.xaml 或 BaseDark.xaml 文件的内容并将其粘贴到您的新主题文件中。
      2. 在应用程序启动时覆盖主题

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
        
            // Add and apply style
            ThemeManager.AddAccent("CustomAccent", new Uri("pack://application:,,,/Custom.Style;component/CustomAccent.xaml"));
            ThemeManager.AddAppTheme("CustomTheme", new Uri("pack://application:,,,/Customo.Style;component/CustomTheme.xaml"));
            ThemeManager.ChangeAppStyle(Application.Current, ThemeManager.GetAccent("CustomAccent"), ThemeManager.GetAppTheme("CustomTheme"));
        }
        
      3. 以更改所有 MetroWindow 的背景颜色为例,更改键“WhiteColor”的值

      【讨论】:

        猜你喜欢
        • 2011-01-23
        • 2020-12-20
        • 1970-01-01
        • 2021-09-24
        • 1970-01-01
        • 2017-08-06
        • 2021-03-26
        • 1970-01-01
        • 2011-04-03
        相关资源
        最近更新 更多