I have simple viewmodel:
private List<Item> itemsList;
public List<Item> ItemsList
{
get { return itemsList; }
set
{
itemsList= value;
NotifyPropertyChanged();
}
}
private Item selectedItem;
public Item SelectedItem
{
get { return selectedItem; }
set
{
selectedItem = value;
NotifyPropertyChanged();
}
}
// I call this method from button handler
// window code behind: vm.SaveChanges()
// instead of RelayCommand because I'm lazy (I don't need strict MVVM)
public void SaveChanges()
{
context.SaveChanges();
}
Im my view I have a ListView:
<ListView
ItemsSource="{Binding ItemsList}"
SelectedItem="{Binding SelectedItem}"
/>
and few controls with Item properties:
<TextBox Text="{Binding SelectedItem.Name}"/>
<TextBox Text="{Binding SelectedItem.Phone}"/>
When I select an Item on a ListView - SelectedItem field values appear in TextBoxes, I can edit them and save them. Everything works fine, but I don't want to allow user to change selected item before he saves changes, because user will not see what was changed in another item(s).
Now I want to disable selection change on ListView, when item has unsaved changes.
I was experimenting with ViewModel property like this:
public bool NoUnsavedChanges
{
get { return !context.ChangeTracker.HasChanges(); }
private set;
}
and binding it to IsEnabled property of ListView, but of course it does not work because there is no NotifyPropertyChanged() for this property.
My question is: how can I disable ListView selection change (or disable it totally) when selected item (or if there are unsaved changes in the entire context)?