Updating Content Binding When Subselections Changed

I have a label like this:

<Label Name="LblUsersWithHair">
    <Binding Path="Users" 
             ElementName="ElementSelf" 
             Converter="{StaticResource Converter_UsersWithHairPresenter}" />
</Label>

And the converter:

...

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    var users = value as ObservableCollection<Users>;
    if (users == null) return null;

    var usersWithHair = users.Count(user => user.HasHair == true);
    return "There are " + usersWithHair + " there has hair.";
}

...

The problem is that the label, of course, is not updated when the HasHair property changes because the collection does not change. But how to make a shortcut regroup when this property is set?

The above example is very simplified, but hope you can help me ...: o)

+3
source share
3 answers

, ListChanged. (//) , , INotifyPropertyChanged. , INotifyPropertyChanged , :

  • ObservableCollection, PropertyChanged-Event ( /) ListChanged .
  • CollectionViewSource , HasHair, CollectionViewSource .
+1

INotifyPropertyChanged Users HasHair.

ListBox ListBoxItems

+2

, , . , , INPC (INotifyPropertyChanged). , , , ObservableCollection, INPC , , / , - HasHair. , , , , .

, :

<Label Content="{Binding Users, Converter={StaticResource Converter_UsersWithHairPresenter}"/>

You can also place the debug breakpoint inside the converter and first see if it will even be called, and also check the output window to see if any data binding errors are being reported.

NTN,
Berryl

0
source

Source: https://habr.com/ru/post/1759521/