創建一個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();
}
}
}
添加一個ResourceDictionary文件 命名:
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)
{
}
}
}