WPF MahApps为其UserControl

时间:2015-08-07 12:33:52

标签: c# wpf xaml user-controls mahapps.metro

我添加了MahApps资源,例如 App.xaml:

    <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>
        ....

它适用于我的所有窗口,但我有一个特殊的窗口(WindowA),我想要使用不同的颜色,所以我将MahApps资源添加到此窗口

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>

现在我想动态地应用我的强调色,所以我使用这样的代码:

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

Amber 的标题栏颜色(WindowA颜色)的结果很酷但是我有一个labelLable 1UserControl内的TestUserControlWindowA),其颜色为Blue

  

WindowA&gt; TestUserControl&gt; Lable 1

enter image description here

lable 1中的TestUserControl xaml标记:

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

我希望将{StaticResource AccentColorBrush} Foreground颜色的所有元素颜色更改为Amber UserControls WindowA内的所有元素AccentColorBrush { {1}}。

我认为StaticResource正在UserControl中使用MahApps资源declared。如何强制它在App.xaml中使用MahApps资源declared

我该如何解决这个问题?

EDIT1

如果我将WindowA.xaml的重音应用于Red,则Application颜色将更改为label 1颜色。

2 个答案:

答案 0 :(得分:1)

将此添加到UserControl.Resources

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

答案 1 :(得分:0)

您应该使用DynamicResource代替StaticResource作为前景。

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

希望有所帮助。