【问题标题】:WPF bind controls to static object[] array data (.NET 4.5)WPF 将控件绑定到静态对象 [] 数组数据 (.NET 4.5)
【发布时间】:2014-02-13 17:33:13
【问题描述】:

我有一个静态对象[] 数组(500 多个项目),每秒更改一次,并且有许多控件需要显示该数组中包含的数据。我需要这个数组是静态的,因为它在许多其他类中使用。

是否可以在 .NET 4.5 中实现这种绑定?我正在尝试下面的代码但没有成功(基于 http://www.jonathanantoine.com/2011/09/28/wpf-4-5-%E2%80%93-part-9-binding-to-static-properties/ )。编译时出现“Playback.Control”未实现接口成员“System.ComponentModel.INotifyPropertyChanged.PropertyChanged”错误

public class Control : INotifyPropertyChanged
{
    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
    public static void RaiseChangeEvent(string propName)
    {
        EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
        if (handler != null)
        handler(null, new PropertyChangedEventArgs(propName));
    }

    private static int _playposition;
    public static int PlayPosition { get { return _playposition; } set { if (_playposition == value) return; _playposition = value; RaiseChangeEvent("PlayPosition"); } }

    public static DataTable JobData { get; private set; }

    private static Object[] _currentdata;
    public static Object[] CurrentData { get { return _currentdata; } set { if (_currentdata == value) return; _currentdata = value; RaiseChangeEvent("CurrentData"); } }

    private static Object[] _previousdata;
    public static Object[] PreviousData { get { return _previousdata; } set { if (_previousdata == value) return; _previousdata = value; RaiseChangeEvent("PreviousData"); } }
}

【问题讨论】:

    标签: c# .net wpf data-binding


    【解决方案1】:

    您链接到的文章有点令人困惑,我怀疑您混淆了这两种方法。查看作者的保管箱项目,似乎有两种方法 - 一种实现 INotifyPropertyChanged 的​​类,另一种 (Repository.cs)。后者使用我认为您正在尝试使用的 StaticPropertyChanged 事件方法。我想您只需要更改代码以使其不实现 INotifyPropertyChanged,但请检查作者的保管箱代码以确保。

    【讨论】:

    • 谢谢!删除 INotifyPropertyChanged 解决了所有问题……我太投入了,以至于我错过了最基本的细节……
    猜你喜欢
    • 1970-01-01
    • 2010-11-09
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多