1
我想添加自定义属性我的用户控制,使得显示(或开启/关闭)的各种选项自定义在WPF用户控件属性
即
<toolkit:UC_TitleBar title="My Application Title" showCloseButton="false" />
当用户控件将然后使用该属性
我该怎么做?
我想添加自定义属性我的用户控制,使得显示(或开启/关闭)的各种选项自定义在WPF用户控件属性
即
<toolkit:UC_TitleBar title="My Application Title" showCloseButton="false" />
当用户控件将然后使用该属性
我该怎么做?
你需要的是什么dependency properties
public class UC_TitleBar : UserControl
{
public static readonly DependencyProperty ShowCloseButtonProperty = DependencyProperty.Register("ShowCloseButton",
typeof(Boolean), typeof(UC_TitleBar), new FrameworkPropertyMetadata(false));
public bool ShowCloseButton
{
get { return (bool)GetValue(ShowCloseButtonProperty); }
set { SetValue(ShowCloseButtonProperty, value); }
}
}
//add dependency property
public static DependencyProperty MyTestProperty;
//init dependency property in static control constructor
static MyControl()
{
var myTestPropertyMetadata = new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender, MyTestPropertyChanged);
MyTestProperty= DependencyProperty.Register("MyTest",
typeof(string),
typeof(MyControl),
myTestPropertyMetadata);
}
//implement property
public String MyTest
{
get { return (String)GetValue(MyTestProperty); }
set
{
SetValue(MyTestProperty, value);
}
}
//using in xaml
<MyControls:MyControl MyTest="dfdsf" />
在MSDN
阅读更多关于依赖属性