【问题标题】:Custom control default style自定义控件默认样式
【发布时间】:2016-09-07 07:14:40
【问题描述】:

我喜欢将我的一些自定义控件分离到一个 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))); 
}

【问题讨论】:

    标签: c# wpf dll styles


    【解决方案1】:

    库中自定义控件的默认样式位于库项目中名为 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 上的Control Authoring Overview 文章以进一步阅读。

    【讨论】:

    • 很好的答案!谢谢你。我的错误是在名为 MyControl.xaml 的文件中定义了我的 RD,但 Themes/Generic.xaml 除外。现在一切都很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2015-12-29
    • 2016-12-14
    相关资源
    最近更新 更多