Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在这种情况下,如何模拟可观察集合的效果?_C#_Wpf_Data Binding_Mvvm_Properties - Fatal编程技术网

C# 在这种情况下,如何模拟可观察集合的效果?

C# 在这种情况下,如何模拟可观察集合的效果?,c#,wpf,data-binding,mvvm,properties,C#,Wpf,Data Binding,Mvvm,Properties,我正在为另一个应用程序制作一个配置编辑器,并使用反射从配置类中提取可编辑字段。下面的类是各种“DataTypeViewModels”的基类,并显示了如何获取和设置适当的属性 public abstract class DataTypeViewModel<T> : ViewModelBase { Func<T> getFunction; Action<T> setAction; public const string ValuePro

我正在为另一个应用程序制作一个配置编辑器,并使用反射从配置类中提取可编辑字段。下面的类是各种“DataTypeViewModels”的基类,并显示了如何获取和设置适当的属性

public abstract class DataTypeViewModel<T> : ViewModelBase
{
    Func<T> getFunction;

    Action<T> setAction;

    public const string ValuePropertyName = "Value";

    public string Label { get; set; }

    public T Value
    {
        get
        {
            return getFunction.Invoke();
        }

        set
        {
            if (getFunction.Invoke().Equals(value))
            {
                return;
            }

            setAction.Invoke(value);

            // Update bindings, no broadcast
            RaisePropertyChanged(ValuePropertyName);
        }
    }

     /// <summary>
    /// Initializes a new instance of the StringViewModel class.
    /// </summary>
    public DataTypeViewModel(string sectionName, string label)
    {
        if (IsInDesignMode)
        {
            // Code runs in Blend --> create design time data.
        }
        else
        {
            Label = label;

            getFunction = new Func<T>(() =>
                {
                    return (T)Settings.Instance.GetType().GetProperty(sectionName).PropertyType.
                        GetProperty(label).GetValue(Settings.Instance.GetType().GetProperty(sectionName).GetValue(Settings.Instance, null), null);
                });

            setAction = new Action<T>(value =>
                {
                    Settings.Instance.GetType().GetProperty(sectionName).PropertyType.GetProperty(label).
                        SetValue(Settings.Instance.GetType().GetProperty(sectionName).GetValue(Settings.Instance, null), value, null);
                });
        }
    }
}
添加到AddItemCommand和RemoveItemCommand,但这不起作用,ListView不会得到更新。执行此操作的正确方法是什么?

实现它类似于NotifyPropertyChanged,但ObservableCollection使用它在插入/删除/重置时发出通知

  public class MyCustomCollection : INotifyCollectionChanged
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }

        public void Add(Object o)
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, o));
        }

        public void Remove(Object o)
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, 0));
        }

        public void Clear()
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void Move(Object o, Int32 newIndex)
        {
            Int32 oldIndex = 0; // can get the old index position using collection.IndexOf(o);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move,
                o, newIndex, oldIndex));
        }

        public Object this[Int32 index]
        {
            get
            {
                return null; // return collection[index];
            }
            set
            {
                Object oldValue = null;  // get old value using collection[index];
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,
                    value, oldValue));
            }
        }
    }

…我从

复制了此命令。我尝试了此操作并更改了NotifyPropertyChanged,但两者均未按预期工作。我在相应的调用中添加了add和remove命令,但我没有创建自定义集合,我使用的是基类中的一个属性,该属性是从子类模板化的列表。因此,基本上,您的问题是您有一个不实现collection changed事件的集合。当它发生变化时,您希望得到通知。如果集合未实现ICollectionChanged,则无法对其调用事件。如果您没有能力更改基本集合,那么黑客是您最好的解决方案。
RaisePropertyChanged("Value");
  public class MyCustomCollection : INotifyCollectionChanged
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }

        public void Add(Object o)
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, o));
        }

        public void Remove(Object o)
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, 0));
        }

        public void Clear()
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void Move(Object o, Int32 newIndex)
        {
            Int32 oldIndex = 0; // can get the old index position using collection.IndexOf(o);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move,
                o, newIndex, oldIndex));
        }

        public Object this[Int32 index]
        {
            get
            {
                return null; // return collection[index];
            }
            set
            {
                Object oldValue = null;  // get old value using collection[index];
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,
                    value, oldValue));
            }
        }
    }