public class Person : INotifyPropertyChanged
{
#region PropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string PropertyName)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
#endregion
public Person(string Name, Brush Backround)
{
this.Name = Name;
this.Backround = Backround;
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged("Name");
}
}
private Brush backround;
public Brush Backround
{
get
{
return backround;
}
set
{
backround = value;
OnPropertyChanged("Backround");
}
}
public override string ToString()
{
return Name;
}
}
class MainWindowViewModel : INotifyPropertyChanged
{
#region PropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string PropertyName)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
#endregion
public MainWindowViewModel()
{
}
/// <summary>
/// Kolekcja wszystkich dostępnych osób
/// </summary>
public IList<Person> People { get; private set; } = new ObservableCollection<Person>();
}
<Window x:Class="Aplikacja.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Aplikacja"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Setters>
<Setter Property="Background" Value="{Binding Path=Backround}"/>
</Style.Setters>
</Style>
</Window.Resources>
<Grid>
<Grid.DataContext>
<local:MainWindowViewModel x:Name="hViewModel"/>
</Grid.DataContext>
<ListBox ItemsSource="{Binding Path=People}">
</ListBox>
</Grid>
</Window>