Dra*_*ake 8 wpf user-controls themes
如何在WPF中创建具有基本默认样式的UserControl,但也可以在需要时轻松主题化?
您是否有一些很好的指南,博客条目或解释此特定主题的示例?
马克,提前谢谢你
在WPF中,主题只是一组XAML文件,每个文件都包含一个ResourceDictionary,它包含适用于应用程序中使用的控件的样式和模板定义.主题文件可能如下所示:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:MyApp.UserControls">
<!-- Standard look for MyUserControl -->
<Style x:Key="Standard" TargetType="{x:Type uc:MyUserControl}">
<Setter Property="Width" Value="22" />
<Setter Property="Height" Value="10" />
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
必须通过向程序集添加以下属性来显式启用对WPF应用程序中的主题的支持:
[assembly: ThemeInfo(
ResourceDictionary.None,
ResourceDictionaryLocation.SourceAssembly
)]
Run Code Online (Sandbox Code Playgroud)
这将指示WPF查找名为themes\generic.xaml的嵌入式资源文件,以确定应用程序控件的默认外观.
请注意,当特定于主题的字典包含单独的文件而不是应用程序的程序集时,样式和模板资源必须使用复合键,该组合键告诉WPF哪个程序集包含样式/模板应用的控件.所以前面的例子应该修改为:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:MyApp.UserControls;assembly=MyApp">
<!-- Standard look for MyUserControl in the MyApp assembly -->
<Style x:Key="{ComponentResourceKey {x:Type uc:MyUserControl}, Standard}">
<Setter Property="Width" Value="22" />
<Setter Property="Height" Value="10" />
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
看这篇文章:http://msdn.microsoft.com/en-us/magazine/cc135986.aspx
它讨论了如何编写可以使用 ControlTemplate 进行更改的控件,例如内置控件。
| 归档时间: |
|
| 查看次数: |
4028 次 |
| 最近记录: |