您好我已经通过一些在计算器的相关问题,但仍读的依赖属性无法弄清楚如何解决我的问题:C#覆盖的系统控制
我需要创造一个CheckableGroupBox控制是与GroupBox相同,但在标题中有一个复选框。它具有以下要求:
原组框具有Header属性是Object类型的,我需要在CheckableGroupBox限制这个属性只能是字符串。
用户可以通过调用myCheckableGroupBox.Header =“some text here”来更改标题的文本(复选框的文本)。
我写了下面的代码为这些提议:
public class CheckableGroupBox : System.Windows.Controls.GroupBox
{
//override the default header property
private static void OnHeaderChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
CheckableGroupBox me = o as CheckableGroupBox;
me.labHeader.Content = e.NewValue.ToString();
}
public CheckableGroupBox() : base()
{
//setup the header of the group box
//create the stack panel, add the checkbox and the label
System.Windows.Controls.StackPanel sp = new System.Windows.Controls.StackPanel();
sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
this.chkHeader = new System.Windows.Controls.CheckBox();
sp.Children.Add(this.chkHeader);
sp.Children.Add(this.labHeader);
//set the header to be the stack panel.
this.Header = sp;
//override the default HeaderProperty
CheckableGroupBox.HeaderProperty.OverrideMetadata(
typeof(object),
new PropertyMetadata("", OnHeaderChangedCallback)
);
}
}
但是,代码不起作用。当我在窗口中添加CheckableGroupBox(在设计视图中)时,它会提示:
无法创建“CheckableGroupBox”的实例,Object的类型必须从DependencyObject派生。
任何想法?