我想向我的用户控件添加自定义属性,以便 UserControl 在显示(或打开/关闭)各种选项时使用该属性
IE
<toolkit:UC_TitleBar title="My Application Title" showCloseButton="false" />
我该怎么做呢?
我想向我的用户控件添加自定义属性,以便 UserControl 在显示(或打开/关闭)各种选项时使用该属性
IE
<toolkit:UC_TitleBar title="My Application Title" showCloseButton="false" />
我该怎么做呢?
你需要的是依赖属性
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 中阅读有关依赖属性的更多信息