问题描述
我喜欢将一些自定义控件分离到dll。
假设我具有以下示例控件:
MyControl.cs
命名空间MyControlsNs {
public class MyControl:ContentControl {
public静态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/演示文稿
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定义默认样式?
我发现此线程,但是集成并没有对我有用:
static MyControl(){
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl),new FrameworkPropertyMetadata( typeof(MyControl)));
}
自定义的默认样式库中的控件位于库项目中名为 Themes
的文件夹中的名为 Generic.xaml
的文件中。 / p>
还请注意,默认的样式资源未设置 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
:
[assembly :ThemeInfo(ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly)]
请参阅文章,以供进一步阅读。
I like to separate some of my custom controls to a dll.
lets assume I have the following example control:
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>
If I'd like to use MyControl I actually do the following:
<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>
My goal is keeping the the definition of RD and Style, when I use it in MyMainViewClass; like this:
<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>
How can I define my default Style for MyControl?I found this thread Creating default style, but integrating didn't work for me:
static MyControl() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
}
A default Style for a custom control in a library is located in a file named Generic.xaml
in a folder named Themes
in your library project.
Note also that a default Style resource does not have the x:Key
attribute set.
<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>
Overriding the default value of the DefaultStyleKey
dependency property of the custom control makes sure that the default Style is actually applied to the correct control type:
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(MyControl),
new FrameworkPropertyMetadata(typeof(MyControl)));
}
Finally, you would also have to set the ThemeInfo
attribute in your library's AssemblyInfo.cs
:
[assembly: ThemeInfo(ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly)]
Please see the Control Authoring Overview article on MSDN for further reading.
这篇关于自定义控件的默认样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!