【发布时间】:2011-12-13 08:06:38
【问题描述】:
我正在尝试将主题应用于一堆 wpf 项目。有一个程序集包含 generic.xaml 和不同的应用程序。据我了解,我不能将 ThemeInfo 属性与 ResourceDictionaryLocation.ExternalLocation 一起使用,因为名称必须与我的程序相同,但我有多个程序...
所以我搜索并发现我只需要将字典作为 MergedDictionary 包含在 app.xaml 中
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ClassLibrary1;component/Themes/generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
这基本上有效。但是,如果我对控件使用样式,它将不再应用 generic.xaml 样式:
ClassLibrary1.dll 中的generic.xaml
<ResourceDictionary x:Class="ClassLibrary1.Themes.generic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Button}">
<Setter Property="Background"
Value="Black" />
</Style>
程序中的窗口
<Window x:Class="Theming.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button"
x:Key="ButtonGreenTextStyle">
<Setter Property="Foreground"
Value="Green" />
</Style>
</Window.Resources>
<Grid>
<Button Style="{DynamicResource ButtonGreenTextStyle}" Content="Test" />
</Grid>
</Window>
我必须做的是,WPF 会将我的 generic.xaml 中的样式理解为所有按钮的基本样式(我知道我还必须编写一个 ControlTemplate;上面的代码只是为了简单起见)
【问题讨论】: