创建一个WPF自定义控件,同时为它添加依赖属性
1. 新建一个解决方案 WpfCustomControlTest
2. 新建一个用户自定义控件的类库 取名为WpfCustom1
将customecontrol1.cs 改名为 TBcontrol.cs,同时修文件里面的类名
注意:原来的控件customecontrol1默认的关联样式在文件 Themes/Generic.xaml中,这时候也要修改这个文件让样式
和新控件名TBcontrol关联起来.
关于 DefaultStyleKeyProperty.OverrideMetadata(typeof(TBControl), new FrameworkPropertyMetadata(typeof(TBControl)));是用来重写自定义控件的元数据,因为WPF不支持父类样式自动应用于
子类。如果屏掉这句话,可以添加一个显示的控件KEY来解决:
<Window.Resources>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="25" />
<Setter Property="Background" Value="Green" />
</Style>
</Window.Resources>
<StackPanel>
<TextBox Text="aaa" />
<local:CustomTextBox Text="bbb" Style="{StaticResource {x:Type TextBox}}" />
</StackPanel>
3. 为新控件注册一个依赖属性 (代码下载类TBControl中)如下
public int NewValue
{
get
{
return (int)this.GetValue(NewValueProperty);
}
set
{
this.SetValue(NewValueProperty, value);
}
}
public static readonly DependencyProperty NewValueProperty = DependencyProperty.Register(
"NewValue",
typeof(int),
typeof(TBControl),
new PropertyMetadata(0));
这样就能在这个控件的属性里面看到这个NewValue了。
4. 下面为这个控件添加设计,新建一个新类库 取名为WpfCustom1.Design
注意:名称必须是控件类库名称+".Design",同时这个类库的build输出地址必须和Step2中的一样
这个类库引用之前创建的WpfCustom1,同时还要引用以下的6个dll:PresentationCore,PresentationFramework,WindowsBase, System.Xaml,Microsoft.Windows.Design.PropertyEditing,Microsoft.Windows.Design.Interaction
为刚创建的新属性NewValue添加可编辑的功能:
4.1. 先添加一个样式
添加文件EditorResources.cs 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace Controls.Design
{
public partial class EditorResources : ResourceDictionary //注意是partial
{
public EditorResources()
: base()
{
InitializeComponent();
}
}
}
EditorResources .xaml
内容如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:PropertyEditing="clr-namespace:Microsoft.Windows.Design.PropertyEditing;assembly=Microsoft.Windows.Design.Interaction"
xmlns:Local="clr-namespace:Controls.Design"
x:Class="Controls.Design.EditorResources">
<DataTemplate x:Key="ComplexInlineEditorTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding StringValue}"/>
<PropertyEditing:EditModeSwitchButton Grid.Column="1"/>
</Grid>
</DataTemplate>
</ResourceDictionary>
4.3 删除Class1.cs文件 ,添加Metadata.cs文件 内容为
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfCustom1.Design;
using Microsoft.Windows.Design.Features;
using Microsoft.Windows.Design.Metadata;
using Microsoft.Windows.Design.PropertyEditing;
[assembly: ProvideMetadata(typeof(WpfCustom1.Design.Metadata))]
namespace WpfCustom1.Design
{
internal class Metadata : IProvideAttributeTable
{
// Accessed by the designer to register any design-time metadata.
public AttributeTable AttributeTable
{
get
{
AttributeTableBuilder builder = new AttributeTableBuilder();
builder.AddCustomAttributes(
typeof(TBControl),
"NewValue",
PropertyValueEditor.CreateEditorAttribute(
typeof( PropertyValueEditor1 )));
return builder.CreateTable();
}
}
}
}
5. 创建PropertyValueEditor1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Design.PropertyEditing;
using System.Windows;
namespace WpfCustom1.Design
{
public class PropertyValueEditor1 : PropertyValueEditor
{
private EditorResources res = new EditorResources();
public PropertyValueEditor1()
{
this.InlineEditorTemplate = res["NewValueInlineEditorTemplate"] as DataTemplate;
}
public override void ShowDialog(
PropertyValue propertyValue,
IInputElement commandSource)
{
}
}
}