绑定到集合计数位置

时间:2018-07-30 14:20:14

标签: c# wpf

我的问题与此类似:Bind to Count of List where Typeof

但这对班级如何起作用?

在MainWindow中,我具有以下集合和Selected Count属性

private ObservableCollection<MyClass> _myClassCollection = new ObservableCollection<MyClass>();
public ObservableCollection<MyClass>
{
    get => _myClassCollection;
    set 
    {
        if(_myClassCollection == value) return;
        _myClassCollection = value;
        OnPropertyChanged("MyClassCollection");
    }
}

public int SelectedCount
{
    get => MyClassCollection.Where(x => x.IsSelected == true).Count();
}

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}

我的MyClass:

public class MyClass : INotifyPropertyChanged
{
    // .. Properties
    private bool _isSelected;
    public bool IsSelected
    {
        get => _isSelected;
        set
        {
            if(_isSelected == value) return;
            _isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}

如果SelectedCount的{​​{1}}属性更改了,那么我如何“运行” IsSelected属性?我想实时显示ObservableCollection的Selected Items的数量。

3 个答案:

答案 0 :(得分:1)

您可以仅在其他可能发生更改的操作的设置器中为SelectedCount添加一个OnPropertyChaned。例如关于设置我的班级集合。这将告诉监听该特定属性的人员可能有所更改,然后重新获取值。

set 
{
    if(_myClassCollection == value) return;
    _myClassCollection = value;
    OnPropertyChanged("MyClassCollection");
    OnPropertyChanged("SelectedCount");  // Make the change Here.
}

从注释看来,您似乎需要收听每个元素的显式更改的属性。这是一个使用事件处理程序的设置程序外观的示例。

set 
{
    // remove subscriptions
    if(_myClassCollection != null) 
    {
       foreach(var element in _myClassCollection)
       {
          element.PropertyChanged -= ElementChanged;
       } 
    }

     // set to new collection
    _myClassCollection = value;

    // subscribe to new elements.
    if(_myClassCollection != null) 
    {
       foreach(var element in _myClassCollection)
       {
          element.PropertyChanged += ElementChanged;
       } 
    }

    OnPropertyChanged("MyClassCollection");
    OnPropertyChanged("SelectedCount");  // Make the change Here.
}



private void ElementChanged(object sender, PropertyChangedEventArgs e)
{
    if(e.PropertyName == nameof(MyClass.IsSelected))
    {
         OnPropertyChanged("SelectedCount");
    }
}

现在,如果您要在集合中添加或删除元素而不创建新集合,则需要在CollectionChanged事件处理程序中订阅或删除订阅。

答案 1 :(得分:1)

您可以使用DynamicData使其更具可读性:

var allObjects = new SourceList<MyClass>(); // this is what you populate with your objects

SelectedObjects = allObjects.Connect().Filter(x => x.IsSelected).AsObservableList();

如果SelectedObjects是公共属性,则可以像这样绑定:

<TextBloc Text="{Binding SelectedObjects.Count}"/>

答案 2 :(得分:0)

您必须处理CollectionChanged中的ObservableCollection。 在那里,您必须像链接的问题中一样致电OnPropertyChanged("SelectedCount")

在您的集合中:

_myClassCollection.CollectionChanged += Handle_CollectionChanged;

在事件处理程序中:

private void Handle_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    OnPropertyChanged("SelectedCount");
}