【问题标题】:Inheritable Dependency Properties可继承的依赖属性
【发布时间】:2013-04-01 02:06:49
【问题描述】:

我有各种各样的用户控件,我正在尝试查看是否可以为它们创建一个具有一些依赖属性的基类。

具体来说,我的大多数用户控件都遵循这种格式...

<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

我在我的解决方案中找不到部分类的其他部分???我已经尝试在整个解决方案中搜索它...

【问题讨论】:

  • 您的问题是什么?看来您已经有了答案。

标签: c# wpf inheritance user-controls dependency-properties


【解决方案1】:

你可以继承。像这样:

  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)。然后,您只需在 ControlTemplate 中定义一次 UI。

【讨论】:

  • 糟糕,复制粘贴了您的代码,但并未更改 XAML 中的根元素。请参阅我在第 3 条中的编辑。
  • 感谢您的编辑...但是如何解决我遇到的部分类错误?谢谢。
  • 德普。谢谢。我错过了您将开始标签更改为&lt;BaseExpanderUC&gt; 的部分。我还注意到this 之前也回答了问题,以防万一有人需要。
猜你喜欢
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多