Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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# Prism中的DelegateCommand,其canExecuteMethod由ObservableCollection中的属性确定。我该如何继续;遵守「;可以吗?_C#_.net_Wpf_Prism - Fatal编程技术网

C# Prism中的DelegateCommand,其canExecuteMethod由ObservableCollection中的属性确定。我该如何继续;遵守「;可以吗?

C# Prism中的DelegateCommand,其canExecuteMethod由ObservableCollection中的属性确定。我该如何继续;遵守「;可以吗?,c#,.net,wpf,prism,C#,.net,Wpf,Prism,我有一个可观察的类集合,它实现了BindableBase和IDataErrorInfo。在我看来,我有一个按钮,它的ICommand绑定应该只在可观察集合中的每个元素都经过验证时才可执行。由于其中一个元素几乎总是以无效开始,因此按钮最初被禁用。我的构造函数中有以下代码: this.StartInspectionCommand = new DelegateCommand(this.StartInspection, () => this.Parameters.All(p => strin

我有一个可观察的类集合,它实现了
BindableBase
IDataErrorInfo
。在我看来,我有一个按钮,它的
ICommand
绑定应该只在可观察集合中的每个元素都经过验证时才可执行。由于其中一个元素几乎总是以无效开始,因此按钮最初被禁用。我的构造函数中有以下代码:

this.StartInspectionCommand = new DelegateCommand(this.StartInspection, () => this.Parameters.All(p => string.IsNullOrEmpty(p["Value"])))
其中,我的可观察集合定义如下:

public ObservableCollection<Parameter> Parameters { get; } = new ObservableCollection<Parameter>();
当用户在各种参数中输入有效值时,重新评估canExecuteMethod的语法是什么?(或者,就此而言,导致当前有效的文件失效。)

我知道如何对属性本身使用
ObservesCanExecute
observeproperty
,但我不确定如何将其应用于属于
observeCollection
的类内的属性


谢谢。

在此场景中,您不使用ObservesProperty或ObservesCanExcute。更新集合中的属性后,必须手动调用StartInspectionCommand.RaiseCanecuteChanged。

在这种情况下,您不使用ObservesProperty或ObservesCanExcute。更新集合中的属性后,您必须手动调用StartInspectionCommand.RaiseCanecuteChanged。

除了@Brian Lagunas'回答之外,您还可以处理
参数
集合中所有项的
属性更改
事件,并随时调用
RaiseCanecuteChanged
方法任何项目的属性都已更改,例如:

public class ViewModel
{
    public ViewModel()
    {
        this.StartInspectionCommand = new DelegateCommand(this.StartInspection, () => this.Parameters.All(p => string.IsNullOrEmpty(p["Value"])))
        this.Parameters.CollectionChanged += Parameters_CollectionChanged;
    }

    public ObservableCollection<Parameter> Parameters { get; } = new ObservableCollection<Parameter>();

    private void Parameters_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (object parameter in e.NewItems)
            {
                (parameter as INotifyPropertyChanged).PropertyChanged
                    += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }

        if (e.OldItems != null)
        {
            foreach (object parameter in e.OldItems)
            {
                (parameter as INotifyPropertyChanged).PropertyChanged
                    -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        StartInspectionCommand.RaiseCanExecuteChanged();
    }

    //...
}
公共类视图模型
{
公共视图模型()
{
this.StartInspectionCommand=new DelegateCommand(this.StartInspection,()=>this.Parameters.All(p=>string.IsNullOrEmpty(p[“Value”]))
this.Parameters.CollectionChanged+=参数\u CollectionChanged;
}
公共ObservableCollection参数{get;}=new ObservableCollection();
私有void参数\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
如果(如NewItems!=null)
{
foreach(e.NewItems中的对象参数)
{
(参数为INotifyPropertyChanged)。属性已更改
+=新的属性变更管理器(项目_属性变更);
}
}
如果(例如,OldItems!=null)
{
foreach(e.OldItems中的对象参数)
{
(参数为INotifyPropertyChanged)。属性已更改
-=新的属性变更管理器(项目_属性变更);
}
}
}
私有无效项\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
StartInspectionCommand.RaiseCanExecuteChanged();
}
//...
}

除了@Brian Lagunas'答案之外,您还可以处理
参数
集合中所有项目的
属性更改
事件,并在任何项目的属性更改时调用
RaiseCanceChanged
方法,例如:

public class ViewModel
{
    public ViewModel()
    {
        this.StartInspectionCommand = new DelegateCommand(this.StartInspection, () => this.Parameters.All(p => string.IsNullOrEmpty(p["Value"])))
        this.Parameters.CollectionChanged += Parameters_CollectionChanged;
    }

    public ObservableCollection<Parameter> Parameters { get; } = new ObservableCollection<Parameter>();

    private void Parameters_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (object parameter in e.NewItems)
            {
                (parameter as INotifyPropertyChanged).PropertyChanged
                    += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }

        if (e.OldItems != null)
        {
            foreach (object parameter in e.OldItems)
            {
                (parameter as INotifyPropertyChanged).PropertyChanged
                    -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        StartInspectionCommand.RaiseCanExecuteChanged();
    }

    //...
}
公共类视图模型
{
公共视图模型()
{
this.StartInspectionCommand=new DelegateCommand(this.StartInspection,()=>this.Parameters.All(p=>string.IsNullOrEmpty(p[“Value”]))
this.Parameters.CollectionChanged+=参数\u CollectionChanged;
}
公共ObservableCollection参数{get;}=new ObservableCollection();
私有void参数\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
如果(如NewItems!=null)
{
foreach(e.NewItems中的对象参数)
{
(参数为INotifyPropertyChanged)。属性已更改
+=新的属性变更管理器(项目_属性变更);
}
}
如果(例如,OldItems!=null)
{
foreach(e.OldItems中的对象参数)
{
(参数为INotifyPropertyChanged)。属性已更改
-=新的属性变更管理器(项目_属性变更);
}
}
}
私有无效项\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
StartInspectionCommand.RaiseCanExecuteChanged();
}
//...
}

如果有任何掘墓人仍在寻找此问题的答案,我已经找到了解决此问题的不同方法,因为我无法让我的ObserviceCollection触发CollectionChanged事件

填充集合后,我手动迭代并为每个项设置propertychanged,如下所示:

OrdersToConfirm = new ObservableCollection<mConfirmedQuote>(_quoteService.GetOrdersToBeConfirmed());
OrdersToConfirm.ToList().ForEach(x => { x.PropertyChanged += item_PropertyChanged; });

private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     ConfirmCommand.RaiseCanExecuteChanged();
}
OrdersToConfirm=newobservecollection([u quoteService.getorderstobeconfirm());
OrdersToConfirm.ToList().ForEach(x=>{x.PropertyChanged+=item_PropertyChanged;});
私有无效项\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
ConfirmCommand.RaiseCanExecuteChanged();
}

我不确定这样做的缺点是什么,如果有,但是如果我遇到任何问题,我会更新。

如果有任何掘墓人仍然在寻找这个问题的答案,我已经找到了解决这个问题的不同方法,因为我无法让我的ObserviceCollection触发CollectionChanged事件

填充集合后,我手动迭代并为每个项设置propertychanged,如下所示:

OrdersToConfirm = new ObservableCollection<mConfirmedQuote>(_quoteService.GetOrdersToBeConfirmed());
OrdersToConfirm.ToList().ForEach(x => { x.PropertyChanged += item_PropertyChanged; });

private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     ConfirmCommand.RaiseCanExecuteChanged();
}
OrdersToConfirm=newobservecollection([u quoteService.getorderstobeconfirm());
OrdersToConfirm.ToList().ForEach(x=>{x.PropertyChanged+=item_PropertyChanged;});
私有无效项\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
ConfirmCommand.RaiseCanExecuteChanged();
}

我不确定这样做的缺点是什么,如果有的话,但如果遇到任何问题,我会更新。

谢谢!你的方法非常有效。(