我有各種各樣的用戶控件,我試圖看看是否可以爲它們創建一些具有某些依賴項屬性的基類。可繼承的依賴屬性
具體來說,我的大部分用戶控件的格式如下...
<UserControl DataContext="{Binding MyDataContext}" >
<Expander IsExpanded="{Binding MyExpandedByDefault}">
<TextBlock>Some text</TextBlock>
</Expander>
</UserControl>
當然,一般來說,如果這只是一次性的,我會在代碼中的依賴屬性後面寫的以上用戶控制。但是,因爲我有一個遵循相同格式的多個用戶控件,我想放像在基類中的下列...
public bool ExpandedByDefault
{
get { return (bool)GetValue(ExpandedByDefaultProperty); }
set { SetValue(ExpandedByDefaultProperty, value); }
}
public static readonly DependencyProperty ExpandedByDefaultProperty =
DependencyProperty.Register("ExpandedByDefault", typeof(bool), typeof(MyBaseView), new UIPropertyMetadata());
我想爲在某處被繼承所以在我主窗口中,我可以做這樣的事情....
<Window>
<StackPanel>
<my:Alpha ExpandedByDefault="True" />
<my:Bravo ExpandedByDefault="False" />
</StackPanel>
</Window>
感謝
編輯:
我已經一個基類,像這樣......
public class ViewBase : UserControl
{
public static readonly DependencyProperty ExpandedByDefaultProperty =
DependencyProperty.Register("ExpandedByDefault",
typeof(bool),
typeof(FiapaDbViewerBase),
new FrameworkPropertyMetadata());
public bool ExpandedByDefault
{
get
{
return (bool)this.GetValue(ExpandedByDefaultProperty);
}
set
{
this.SetValue(ExpandedByDefaultProperty, value);
}
}
}
但是當我嘗試在代碼中繼承其背後爲我,像這樣的用戶控件....
public partial class MyUserControl : ViewBase
{
public MyUserControl()
{
InitializeComponent();
}
}
我得到一個錯誤說
Partial declarations of 'MyUserControl' must not specify different base classes
而我無法在我的解決方案中找到部分類的其他部分?我試過在整個解決方案中搜索它...
你的問題是什麼?這看起來像你已經有了答案。 – 2013-04-09 15:15:23