【问题标题】:Set dependency properties in XAML when the base class is generic当基类是泛型时在 XAML 中设置依赖项属性
【发布时间】:2010-09-02 17:03:53
【问题描述】:

正如标题所说,当基类是泛型时,如何在 XAML 中设置依赖属性?尝试执行此操作时,我得到一个 NullReferenceException,从后面的代码设置属性可以正常工作。当基类不是通用的时,它也可以工作。 我正在使用 .NET4

这里有一些示例代码来演示:

WindowBase.cs

using System.Windows;

namespace GenericDependencyPropertyTest
{
    public class WindowBase<ViewModel> : Window
    {
        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(
        "Header", typeof(string), typeof(WindowBase<ViewModel>), new PropertyMetadata("No Header Name Assigned"));

        public string Header
        {
            get { return (string)GetValue(HeaderProperty); }
            protected set { SetValue(HeaderProperty, value); }
        }
        protected virtual ViewModel Model { get; set; }
    }
}

MainWindow.xaml

<local:WindowBase x:Class="GenericDependencyPropertyTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:GenericDependencyPropertyTest"
        x:TypeArguments="local:IMyViewModel"
        Title="MainWindow" Height="350" Width="525" Header="Test">
    <Grid>

    </Grid>
</local:WindowBase>

MainWindow.xaml.cs

namespace GenericDependencyPropertyTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : WindowBase<IMyViewModel>
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        protected override IMyViewModel Model
        {
            get
            {
                return base.Model;
            }
            set
            {
                base.Model = value;
            }
        }
    }
}

IMyViewModel.cs

namespace GenericDependencyPropertyTest
{
    public interface IMyViewModel
    {
    }
}

【问题讨论】:

  • “当基类是泛型时”是什么意思?你是想说自定义控件吗?
  • 抱歉不清楚。我的意思是从通用类继承的控件(或本例中的窗口),在上面的示例中,我传递了一个接口,用于窗口将具有的视图模型类型,因为 MainWindow 继承自 WindowBase。

标签: c# wpf generics xaml .net-4.0


【解决方案1】:

我的猜测是,问题出在依赖属性的所有者类型 (typeof(WindowBase)。每个封闭的泛型类型将是完全不同的运行时类型,因此 compiler 是很高兴该属性存在,当 WPF 运行时根据其内部存储中的(不同)类型查找该属性时找不到它。

如您所见,非泛型类之所以有效,是因为所有者类型和运行时类型相同。

您可以通过将 DP 推送到非泛型基类来获得您想要的行为,但仍然从泛型类派生您的视图以获取强类型模型

public class WindowBase : Window
{
    public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(
        "Header", typeof(string), typeof(WindowBase), new PropertyMetadata("No Header Name Assigned"));

    public string Header
    {
        get { return (string)GetValue(HeaderProperty); }
        protected set { SetValue(HeaderProperty, value); }
    }
}

public class WindowBase<ViewModel> : WindowBase
{
    protected ViewModel Model { get; set; }
}

【讨论】:

  • 我们来到了 2018 年,这对我帮助很大。将来我会以这种方式使用泛型来处理我的所有类。我想你们都已经转向更大更好的事情了!
  • @miriyo 很高兴听到该网站作为历史知识库的其中一项工作!即使我们大部分时间都从 WPF 开始,泛型类中的静态范围也没有改变。
猜你喜欢
  • 1970-01-01
  • 2015-11-03
  • 2016-06-02
  • 2012-06-01
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
相关资源
最近更新 更多