【问题标题】:Bind to static generic instance of a class绑定到类的静态泛型实例
【发布时间】:2013-05-15 22:27:47
【问题描述】:

我在我的视图(UserControl)构造函数中使用此代码:

this.DataContext = The<Chart1ViewModel>.Instance;

The&lt;&gt; 是一个通用的 static 每个类型的单例存储,它还需要实现 INotifyPropertyChanged 以通知它的实例替换。

我想对这段代码进行 XAML 处理,我该怎么做?我以前从未将任何东西绑定到通用静态类。

如果我不能,我如何为该静态类的Instance 属性创建一个Binding?

【问题讨论】:

    标签: wpf generics binding singleton static-classes


    【解决方案1】:

    据我所知,在 XAML 中使用泛型会遇到问题。不过,您可以使用静态类。通过在泛型周围包装一个属性,您可以执行以下操作:

    <Control DataContext="{x:Static The.Chart1ViewModel.Instance}" />
    

    【讨论】:

    • 问题是可能存在未知类型,而不仅仅是Chart1ViewModel。
    • 你需要几个新的属性。您还可以检查在 XAML 中使用泛型的方法,但到目前为止我还没有获得这方面的经验:msdn.microsoft.com/en-us/library/ee956431.aspx
    猜你喜欢
    • 2021-08-27
    • 2013-08-25
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多