簡體   English   中英

自定義控件的默認樣式

[英]Custom control default style

我喜歡將一些自定義控件分離為dll。

假設我有以下示例控件:

MyControl.cs

namespace MyControlsNs {
    public class MyControl : ContentControl {
        public static DependencyProperty IsGreatProperty =
            DependencyProperty.Register("IsGreat",
                                    typeof (bool),
                                    typeof (MyControl),
                                    new PropertyMetadata(true));
        public bool IsGreat { 
            get { return (bool) GetValue(IsGreatProperty); }
            set { SetValue(IsGreatProperty, value); }
        }
    }
}

MyControl.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:MyControlsNs"> 
    <Style x:Key="MyControl" TargetType="controls:MyControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <CheckBox IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsGreat}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

如果我想使用MyControl,實際上可以執行以下操作:

<UserControl x:Class="MyMainViewClass"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:controls="clr-namespace:MyControlsNs">
    <UserControl.Resources>
        <ResourceDictionary Source="MyControl.xaml" />
    </UserControl.Resources>
    <controls:MyControl IsGreat="true" Style="{StaticResource MyControl}" />
</UserControl>

我的目標是在MyMainViewClass中使用RD和樣式的定義。 像這樣:

<UserControl x:Class="MyMainViewClass"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:controls="clr-namespace:MyControlsLib.MyControlsNs;assembly=MyControlsLib">
    <controls:MyControl IsGreat="true" />
</UserControl>

如何為MyControl定義默認樣式? 我發現此線程正在創建默認樣式 ,但集成對我而言不起作用:

static MyControl() {
   DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl))); 
}

庫中自定義控件的默認樣式位於庫項目中名為Themes的文件夾中的Generic.xaml文件中。

還請注意,默認的樣式資源沒有設置x:Key屬性。

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:MyControlsNs"> 
    <Style TargetType="controls:MyControl">
        ...
    </Style>
</ResourceDictionary>

覆蓋自定義控件的DefaultStyleKey依賴項屬性的默認值可確保將默認樣式實際應用於正確的控件類型:

static MyControl()
{
    DefaultStyleKeyProperty.OverrideMetadata(
        typeof(MyControl),
        new FrameworkPropertyMetadata(typeof(MyControl))); 
}

最后,您還必須在庫的AssemblyInfo.cs設置ThemeInfo屬性:

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

請參閱MSDN上的“ 控件創作概述”文章以進一步閱讀。

暫無
暫無

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

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