Here's the code I'm working with. In xaml
I've expanded the ListView
like this:
<ListView x:Name="lv" ItemsSource="{Binding}">
<ListView.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding Edit}" CommandParameter="{Binding SelectedItem, ElementName=lv}"/>
<KeyBinding Key="Return" Command="{Binding Edit}" CommandParameter="{Binding SelectedItem, ElementName=lv}"/>
</ListView.InputBindings>
</ListView>
and implemented INotifyPropertyChanged
in Master
class to see updates in ListView
when I edit an item. In Person
class I've added one more Command
public Command Edit { get; set; }
intialized it in constructior:
Edit = new Command(CanClick, EditItem);
and implemented those callbacks like this:
bool CanClick(object arg) => Count > 0;
void EditItem(object obj) {
if(obj != null{
var item = obj as Master;
item.FirstName = "Edited";
item.LastName = "Edited";
SetItem(IndexOf(item), item);
}
}
When I select an item and hit Return it updates the collection BUT I don't see any change in ListView
. If I double click on an item, it neither updates the collection nor the ListView
!
Another question is why do I have to set a name for the ListView
to use in nested InputBindings
like this ElementName=lv
? Can I get rid of that?
EDIT
If I expand the ListView
further like this:
<ListView x:Name="lv" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" Width="200"
DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Header="Last Name" Width="200"
DisplayMemberBinding="{Binding LastName}" />
</GridView>
</ListView.View>
<ListView.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding Edit}" CommandParameter="{Binding SelectedItem, ElementName=lv}"/>
<KeyBinding Key="Return" Command="{Binding Edit}" CommandParameter="{Binding SelectedItem, ElementName=lv}"/>
</ListView.InputBindings>
</ListView>
ListView
reflects the update I make through void EditItem(object obj)
BUT MouseBinding
doesn't work in this way either. Why would one bind individual property like this for a collection?