[英]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
,但我有一個label
( Lable 1
)在UserControl
( TestUserControl
內) WindowA
和它的顏色是Blue
呢!
WindowA> TestUserControl>標簽1
TestUserControl
lable 1
xaml標簽:
<Label Foreground="{StaticResource AccentColorBrush}">Lable1</Label>
我想改變所有元素的顏色與{StaticResource AccentColorBrush}
Foreground
顏色以Amber
包括里面的所有要素UserControls
的的WindowA
與AccentColorBrush
StaticResource
。
我認為UserControl
正在使用App.xaml
declared
MahApps
資源。 我怎樣才能迫使它使用MahApps
資源declared
在WindowA.xaml
。
我怎樣才能解決這個問題?
編輯1
如果我向Application
像Red
這樣的強調,則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.