我有各种各样的用户控件,我试图看看是否可以为它们创建一些具有某些依赖项属性的基类。可继承的依赖属性
具体来说,我的大部分用户控件的格式如下...
<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