简体   繁体   English

WPF MahApps更改窗口的样式,使其用户控件内的元素不起作用

[英]WPF MahApps Change style of Window notworking for elements inside its UserControl

I added MahApps resources like in App.xaml: 我在App.xaml中添加了MahApps资源

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml" />

            <!-- accent resource -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/blue.xaml" />

            <!-- theme resource -->
            <!-- change "BaseLight" to the theme you want -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/basedark.xaml" />              

        </ResourceDictionary.MergedDictionaries>
        ....

It works well on all of my windows but i have a special window ( WindowA ) that i want be in a different color so i added the MahApps resources to this window 它在我所有的窗口上都可以正常工作,但是我有一个特殊的窗口( WindowA ),我希望将其设置为其他颜色,因此我将MahApps资源添加到了该窗口

WindowA.xaml: WindowA.xaml:

    <controls:MetroWindow.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml" />

            <!-- accent resource -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/blue.xaml" />

            <!-- theme resource -->
            <!-- change "BaseLight" to the theme you want -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/basedark.xaml" />              

        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </controls:MetroWindow.Resources>

Now i want apply my accent color dynamically to it, so i use some codes like this: 现在我想将其强调色动态地应用到它,所以我使用了一些像这样的代码:

ThemeManager.ChangeAppStyle(appOrWindow,
         ThemeManager.GetAccent("Amber"),
         ThemeManager.GetAppTheme("basedark"));

The result is cool for title bar color ( Amber color) of the WindowA BUT i have a label ( Lable 1 ) in a UserControl ( TestUserControl ) inside WindowA and its color is Blue yet! 其结果是凉快标题栏颜色( Amber的颜色) WindowA ,但我有一个labelLable 1 )在UserControlTestUserControl内) WindowA和它的颜色是Blue呢!

WindowA > TestUserControl > Lable 1 WindowA> TestUserControl>标签1

在此处输入图片说明

The lable 1 xaml tag inside TestUserControl : TestUserControl lable 1 xaml标签:

 <Label Foreground="{StaticResource AccentColorBrush}">Lable1</Label>

I want change all element colors with {StaticResource AccentColorBrush} Foreground color to Amber include all elements inside UserControls of the WindowA with AccentColorBrush StaticResource . 我想改变所有元素的颜色与{StaticResource AccentColorBrush} Foreground颜色以Amber包括里面的所有要素UserControls的的WindowAAccentColorBrush StaticResource

I think the UserControl is using MahApps resources declared in App.xaml . 我认为UserControl正在使用App.xaml declared MahApps资源。 How can i force it to use MahApps resources declared in WindowA.xaml . 我怎样才能迫使它使用MahApps资源declaredWindowA.xaml

How can i fix this? 我怎样才能解决这个问题?


Edit1 编辑1

If i apply an accent like Red to the Application the label 1 color will be changed to the Red color. 如果我向ApplicationRed这样的强调,则label 1颜色将更改为Red

将此添加到UserControl.Resources

<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/amber.xaml" />

You should use DynamicResource instead StaticResource for the foreground. 您应该使用DynamicResource而不是StaticResource作为前景。

<Label Foreground="{DynamicResource AccentColorBrush}">Lable1</Label>

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM