I'm trying to update a Listbox item background independently using command as following: Model
public class Item
{
public Item()
{
this.BackColor = new SolidColorBrush(Colors.WhiteSmoke);
}
public int Number { get; set; }
public int Duration { get; set; }
public string Name { get { return Number.ToString(); } }
public SolidColorBrush BackColor { get; set; }
}
ViewModel
private ObservableCollection<Item> _items;
public ObservableCollection<Item> Items
{
get { return _items; }
set { OnPropertyChange(ref _items, value); }
}
Button command
public ICommand StartCommand { get; set; }
public void start()
{
foreach (var item in Items
{
item.BackColor = new SolidColorBrush(Colors.LightGreen);
// Do some work .....
}
}
CTOR
public MainViewModel()
{
StartCommand = new RelayCommand(start);
LoadItems();
}
Xaml
<ListBox ItemsSource="{Binding Items}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Border
Height="50"
BorderThickness="1"
BorderBrush="Silver"
CornerRadius="5"
Margin="10,10,10,5"
Background="{Binding BackColor,
UpdateSourceTrigger=PropertyChanged}">
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I can update the observable collection data, remove items but not change item background .Any help please.
"Update" : As you can see I don't want to change the color for the whole list box, so I can not bind to a common property like "ItemBackground" instead I must bind to the collection object propert "BackColor"