2

I think it should be simple but can't believe how to do.

I try bind in label the count of object that have his property isWorking set at True

Here my collection.

public readonly ObservableCollection<ComputerModel.ControleData> _ComputerList =
             new ObservableCollection<ComputerModel.ControleData>();
public ObservableCollection<ComputerModel.ControleData> ComputerList { get { return _ComputerList; } }

The result I need in a label is like

int workingItems= ComputerList.Where(x=> x.isWorking == true).Count()

Then bind in label

<Label Content="{Binding workingItems}" HorizontalAlignment="Left" Margin="12,424,0,0" VerticalAlignment="Top" Height="22" Width="62"/>

But what is the right method to have this working?? I can't had condition in the WPF itself?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Zwan
  • 632
  • 2
  • 6
  • 23
  • Do you bind workingItems with DataContext? – Safe Mar 15 '17 at 13:13
  • The solution of @S.Akbari is the way to bind `Count` (though I'd put predicate into `Count()` overload and use getter-only property). The only problem still is to get notified when collection is changed to re-evalute value. You can subscribe to `ObservableCollection` [events](https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx) and just rise notification for new `WorkingItems` property (see `INotifyPropertyChanged`), then your binding will re-evalute it and correct value will be displayed by the view. – Sinatr Mar 15 '17 at 13:30
  • Here an example to implement interface `INotifyPropertyChanged` [I'm here!](http://stackoverflow.com/a/42626834/7109040) – Safe Mar 15 '17 at 13:41

2 Answers2

3

You can create a get-only property

public int WorkingItems
{
    get { return ComputerList.Where(x=> x.isWorking == true).Count(); }
}

Now you need to call your implementation of INotifyPropertyChanged whenever any isWorking property inside the ComputerList or the list itself changes.

The following is some pseudo-code to give you an idea what you need to consider in order to notify any possible update. It is not recommended nor complete to handle the notifications exactly like this.

ComputerList.CollectionChanged += (s, e) => NotifyPropertyChanged("WorkingItems");
this.PropertyChanged += (s, e) => { if (e.PropertyName == "ComputerList") NotifyPropertyChanged("WorkingItems"); };
foreach (var item in ComputerList)
{
    item.PropertyChanged += (s, e) => { if (e.PropertyName == "isWorking") NotifyPropertyChanged("WorkingItems"); };
}
grek40
  • 13,113
  • 1
  • 24
  • 50
0

You can't bind to field but properties. So create a property for workingItems and then bind to it:

public int WorkingItems
{
    get { return workingItems; }
    set { workingItems = value; }
}

int workingItems= ComputerList.Where(x=> x.isWorking == true).Count();

And:

<Label Content="{Binding WorkingItems}" .... />
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • and how you set WorkingItem with the count since collection properties change? i did this at first but the real probleme is seting the value – Zwan Mar 15 '17 at 13:09
  • @Zwan Just try this with property as I shown. Set `WorkingItem` with the count as before but set the `Content="{Binding WorkingItems}"`. `WorkingItems` is property. – Salah Akbari Mar 15 '17 at 13:09
  • you totaly ignore collection count in your exemple show where you set WorkingItems it should be binded to select condition – Zwan Mar 15 '17 at 13:11
  • @Zwan See my updated answer. – Salah Akbari Mar 15 '17 at 13:12
  • i gave a pseudo code for condition probleme is not set it once but set WorkingItems rigth when properties in collection change – Zwan Mar 15 '17 at 13:13
  • the perfect code would be but it not exist – Zwan Mar 15 '17 at 13:13
  • @Zwan http://stackoverflow.com/questions/842575/why-does-wpf-support-binding-to-properties-of-an-object-but-not-fields – Salah Akbari Mar 15 '17 at 13:14