2009-02-23 51 views
8

如何在WPF中創建具有基本默認樣式但可以在需要時輕鬆創建主題的UserControl?WPF中的可編程用戶控件

您是否有一些很好的指導方針,博客條目或例子來解釋這個特定主題?

預先感謝您,本文 馬爾科

回答

7

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

支持在WPF應用程序的主題必須通過添加以下屬性大會明確啓用:

[assembly: ThemeInfo(
    ResourceDictionary.None, 
    ResourceDictionaryLocation.SourceAssembly 
)] 

這將指示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> 
+0

「好樣」的鏈接是死 – 2013-02-01 16:05:37