Reputation: 6562
I have a ListView that has an ObservableCollection as it's ItemsSource. The ListView contains multiple columns along with one being a CheckBox. When I select a row I would like the CheckBox to become either Checked or UnChecked depending on it's current value instead of having to click directly on the CheckBox
Here is what I have so far.
private void lstIndexes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lstIndexes.SelectedItem != null)
{
if (lstIndexes.SelectedItem is IndexData)
{
IndexData index = lstIndexes.SelectedItem as IndexData;
if (index.IsChecked)
{
index.IsChecked = false;
}
else
{
index.IsChecked = true;
}
}
}
}
Upvotes: 0
Views: 64
Reputation: 81283
You need to implement INotifyPropertyChanged interface on class IndexData
so that UI gets notified on any property change in underlying source object.
ObservableCollection only updates UI, when item is added or deleted from collection. To refresh UI on any property change in underlying object, INPC is a way to go.
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
if(isChecked != value)
{
isChecked = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("IsChecked");
}
}
}
Upvotes: 3