ObservableCollection <T> does not update ListBox

I installed ItemsSourcefrom ListBoxto a collection ObservableCollection<Employee>, and my class Employeeimplements INotifyPropertyChanged.

In EmployeeI connected several properties, one of which is a property Color, and I was convinced that it raises an event PropertyChangedwhen it changes. I also checked with a debugger that the call was invoked PropertyChanged.

However, when the data binding Backgroundto ListBoxItemthe bound ListBoxdoes never update , which is extremely amazing.

Setting it ItemsSourceto null and resetting it after work, but this does not mean that we should use the Observer pattern.

Used XAML:

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                         Color="{x:Static SystemColors.HighlightColor}" />
    </Style.Resources>
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background"
            Value="{Binding Path=Color, Converter={StaticResource ColorConverter}}" />
    <Setter Property="Content" Value="{Binding Path=Name}" />
    <Setter Property="Height" Value="25" />
    <Setter Property="Margin" Value="0,1,0,1" />
    <EventSetter Event="MouseDoubleClick" Handler="HelperList_MouseDoubleClick" />
</Style>

<ListBox x:Name="helperList" Grid.Column="0" Grid.Row="1" 
         Margin="5,2,0,5" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
         ScrollViewer.VerticalScrollBarVisibility="Visible"
         SelectionChanged="HelperList_SelectionChanged">
</ListBox>

More code in response to the first answer:

public class Employee : Person
{
    private EmployeeColor color = new EmployeeColor();
    public EmployeeColor Color
    {
        get { return this.color; }
        set
        {
            if(!this.color.Equals(value))
                OnPropertyChanged("Color");

            this.color = value;
        }
    }
}

var employees = new ObservableCollection<Employee>();
//... fill out data here    
helperList.ItemsSource = employees;
+3
1

.

OnPropertyChanged , , .

: OnPropertyChanged .

+2

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


All Articles