2013-04-09 70 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