2013-04-09 74 views
1

我有各种各样的用户控件,我试图看看是否可以为它们创建一些具有某些依赖项属性的基类。可继承的依赖属性

具体来说,我的大部分用户控件的格式如下...

<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 

而我无法在我的解决方案中找到部分类的其他部分?我试过在整个解决方案中搜索它...

+0

你的问题是什么?这看起来像你已经有了答案。 – 2013-04-09 15:15:23

回答

2

你可以继承。像这样:

  1. 定义基类:

    public class BaseExpanderUC : 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(false)); 
    } 
    
  2. 定义一个继承的类:

    public class Alpha : BaseExpanderUC{} 
    public class Bravo : BaseExpanderUC{} 
    
  3. 在每各继承类的XAMLs(阿尔法和Bravo以上的),使用这个makup:

    <BaseExpanderUC> 
        <Expander IsExpanded="{Binding MyExpandedByDefault, 
                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:BaseExpanderUC}}}"> 
         <TextBlock>Some text</TextBlock> 
        </Expander> 
    </BaseExpanderUC> 
    

    其中“local”是BaseExpanderUC命名空间的xmlns。

这将要求您为每个UC定义UI。如果您可以为所有控件创建一个通用的UI,那么我强烈建议您使用自定义控件(可能继承了Expander)。然后,您只需要在ControlTemplate中定义一次UI。

+0

请参阅我上面关于我遇到的错误的编辑。 – imdandman 2013-04-09 15:43:35

+0

糟糕,复制粘贴您的代码,并未更改XAML中的根元素。请参阅第3章中的编辑。 – XAMeLi 2013-04-09 15:46:35

+0

感谢您的编辑......但是,如何解决我所得到的部分类错误?谢谢。 – imdandman 2013-04-09 16:29:17