自定义控件默认样式

时间:2016-09-07 07:14:40

标签: c# wpf dll styles

我想将一些自定义控件分隔为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和Style的定义;像这样:

<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定义默认样式? 我找到了这个帖子Creating default style,但是整合并不适合我:

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

1 个答案:

答案 0 :(得分:2)

库中自定义控件的默认样式位于库项目中名为Generic.xaml的文件夹中名为Themes的文件中。

另请注意,默认样式资源没有设置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上的Control Authoring Overview文章,以便进一步阅读。