簡體   English   中英

WPF MahApps更改窗口的樣式,使其用戶控件內的元素不起作用

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

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

它在我所有的窗口上都可以正常工作,但是我有一個特殊的窗口( 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 1 )在UserControlTestUserControl內) WindowA和它的顏色是Blue呢!

WindowA> TestUserControl>標簽1

在此處輸入圖片說明

TestUserControl lable 1 xaml標簽:

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

我想改變所有元素的顏色與{StaticResource AccentColorBrush} Foreground顏色以Amber包括里面的所有要素UserControls的的WindowAAccentColorBrush StaticResource

我認為UserControl正在使用App.xaml declared MahApps資源。 我怎樣才能迫使它使用MahApps資源declaredWindowA.xaml

我怎樣才能解決這個問題?


編輯1

如果我向ApplicationRed這樣的強調,則label 1顏色將更改為Red

將此添加到UserControl.Resources

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

您應該使用DynamicResource而不是StaticResource作為前景。

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

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM