1

さまざまなユーザー コントロールがあり、いくつかの依存関係プロパティを使用してそれらの基本クラスを作成できるかどうかを確認しようとしています。

具体的には、私のユーザーコントロールのほとんどはこの形式に従います...

<UserControl DataContext="{Binding MyDataContext}" >
    <Expander IsExpanded="{Binding MyExpandedByDefault}">
        <TextBlock>Some text</TextBlock>
    </Expander>
</UserControl>

もちろん、通常、これが 1 回限りの場合は、上記のユーザー コントロールのコード ビハインドに依存関係プロパティを記述します。しかし、同じ形式のユーザーコントロールが複数あるので、以下のようなものを基底クラスに入れたいのですが...

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

そして、ソリューションで部分クラスの他の部分が見つかりませんか??? ソリューション全体で検索してみました...

4

1 に答える 1

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. 継承された各クラス (上記の Alpha および Bravo) の XAML のそれぞれで、次の 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 を継承) を使用することを強くお勧めします。次に、UI を .xml で 1 回だけ定義する必要がありますControlTemplate

于 2013-04-09T15:16:05.990 に答える