您好我已經通過一些在計算器的相關問題,但仍讀的依賴屬性無法弄清楚如何解決我的問題: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派生。
任何想法?