ItemsSourceのListBoxをObservableCollection<Employee>コレクションに設定し、Employeeクラスに を実装しINotifyPropertyChangedました。
ではEmployee、いくつかのプロパティをバインドしました。そのうちの 1 つはColorプロパティであり、イベントが変更されたときにイベントが呼び出されるようにしましたPropertyChanged。PropertyChangedまた、呼び出しが呼び出されることをデバッガーで確認しました。
ただし、データバインドされている場合、バインドされた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;