C# 可观察收集<;T>;不更新列表框

C# 可观察收集<;T>;不更新列表框,c#,wpf,data-binding,listbox,observablecollection,C#,Wpf,Data Binding,Listbox,Observablecollection,我已经将列表框的ItemsSource设置为observedcollection集合,我的Employee类实现了INotifyPropertyChanged 在Employee上,我绑定了几个属性,其中一个是Color属性,我确保它在更改时调用PropertyChanged事件。我还检查了调试器是否调用了PropertyChanged调用 但是,当数据绑定到绑定的列表框中的列表框项的背景时,从不更新,这是非常模糊的 将ItemsSource设置为null,并在工作后重置它,但这不是我们使用观察

我已经将
列表框的
ItemsSource
设置为
observedcollection
集合,我的
Employee
类实现了
INotifyPropertyChanged

Employee
上,我绑定了几个属性,其中一个是
Color
属性,我确保它在更改时调用
PropertyChanged
事件。我还检查了调试器是否调用了
PropertyChanged
调用

但是,当数据绑定到绑定的
列表框
中的
列表框项
背景
时,从不更新,这是非常模糊的

ItemsSource
设置为null,并在工作后重置它,但这不是我们使用观察者模式的方式

使用的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>

响应第一次答复的更多代码:

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;
公共类员工:个人
{
private EmployeeColor=新员工颜色();
公共雇员颜色
{
获取{返回this.color;}
设置
{
如果(!this.color.Equals(value))
不动产变更(“颜色”);
这个颜色=值;
}
}
}
var employees=新的ObservableCollection();
//... 在这里填写数据
helperList.ItemsSource=员工;
问题已解决

OnPropertyChanged在设置属性的实际值之前被调用,因此UI将根据旧值进行相应更新


解决方案:设置属性值后调用OnPropertyChanged。

这里没有问题,您能提供更多代码吗?雇主类别以及如何设置ItemSource?完成后,检查更新的代码。正如我所说,这应该是相当明显的,我只是将ItemSource属性设置为ObserverableCollection!你的色彩转换器里有什么?是否按照背景属性的预期将颜色转换为笔刷?我怀疑问题就在那里。它也不适用于任何其他财产。是的,一切都很好。正如我前面所说的,将ItemSource设置为null,然后再设置为相同的ObserveableCollection,可以使其工作。然而,这不是应该怎么做的。听起来合乎逻辑。:)属性仅在设置值后更改。:)还有一个InotifyPropertyChange接口。。。