3

ItemsSourceListBoxObservableCollection<Employee>コレクションに設定し、Employeeクラスに を実装しINotifyPropertyChangedました。

ではEmployee、いくつかのプロパティをバインドしました。そのうちの 1 つはColorプロパティであり、イベントが変更されたときにイベントが呼び出されるようにしましたPropertyChangedPropertyChangedまた、呼び出しが呼び出されることをデバッガーで確認しました。

ただし、データバインドされている場合、バインドされたBackgroundの が更新ListBoxItemされListBoxないため、非常にイライラします。

を null に設定ItemsSourceし、動作後にリセットすることはできますが、これは Observer パターンを利用する方法ではありません。

使用される 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;
4

1 に答える 1

2

問題が解決しました。

プロパティの実際の値が設定される前に OnPropertyChanged が呼び出されたため、UI は古い値に応じて更新されていました。

解決策:プロパティ値を設定した、 OnPropertyChanged を呼び出します。

于 2009-11-20T16:07:09.883 に答える