0

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?

  • 1
    Your collection should also implement `INotifyPropertyChanged` – Aakanksha Oct 14 '19 at 04:00
  • 1
    Person is an observablecollection of master? This is a very strange way to work. Much more usual to have a viewmodel set as datacontext. That would have an observablecollection of person viewmodels. All viewmodels implement inotifypropertychanged. Bind selecteditem to another property of the viewmodel. Then your command can work with that selected item. – Andy Oct 14 '19 at 07:39
  • 1
    As Andy describes: You should also implemente INotifyPropertyChanged to the Person class. – Fruchtzwerg Oct 14 '19 at 07:56
  • @Andy, that's what it is I think: `Person` is in `DataContext`, it is an `ObservableCollection` and both `Person` and `Master` implemented `INotifyPropertyChanged`. When I hit `Return` after selecting an item, `void EditItem(object obj)` gets the `SelectedItem`. –  Oct 14 '19 at 11:38
  • @EmonHaque: What does "updates the collection BUT I don't see any change in ListView" mean exactly? – mm8 Oct 14 '19 at 11:59
  • @mm8, if I launch the app in debug mode after setting a breakpoint at the end of `EditItem` method, select an item in the `ListView` and hit `Return`. `this` pointer in `Autos` window shows the updated list BUT the `ListView` doesn't reflect the change! –  Oct 14 '19 at 12:08
  • @EmonHaque: Why are you calling `SetItem`? – mm8 Oct 14 '19 at 12:09
  • @mm8, isn't it for editing an item in the list? –  Oct 14 '19 at 12:10
  • You edit an item by setting its properties and implementing `INotifyPropertyChanged`. Does `Master` implement this interface? – mm8 Oct 14 '19 at 12:11
  • @mm8, it works this way. –  Oct 14 '19 at 12:23

1 Answers1

0

There is no reason to call SetItem to update an item.

Setting the properties is enough provided that the Master class implements INotifyPropertyChanged correctly.

When it comes to handling double-clicks, you could add a reference to System.Windows.Interactivity and use an EventTrigger:

<ListView x:Name="lv" ItemsSource="{Binding}" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding Edit}" CommandParameter="{Binding SelectedItem, ElementName=lv}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <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>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • that's right, I actually don't need that BUT mousebinding doesn't work. If I don't bind individual properties like I did in EDIT part, listview doesn't reflect the change. –  Oct 14 '19 at 12:29
  • @EmonHaque: See my edit about using an `EventTrigger` to handle double-clicks. – mm8 Oct 14 '19 at 12:33
  • lol, to avoid the blend iteractivity triggers I'd added `Xaml.Behaviors` nuget package to make it simpler! looks like it still is a must to use blend interactivity for this! Doubleclick works when I click on the borders of `ListView`, there's a small margin/padding around the list in listview, it triggers when I double click on that blank area! –  Oct 14 '19 at 12:39
  • It triggers when the `ListView` raises the `MouseDoubleClick` event as you would expect. – mm8 Oct 14 '19 at 12:57
  • I'd expect the same event to be raised when I doubleclick on a listitem in the ListView. KeyBinding does what is expected BUT the MouseBinding doesn't! –  Oct 14 '19 at 15:06
  • @EmonHaque: So use an event trigger. – mm8 Oct 14 '19 at 15:08
  • Yes, that's probably the only option for now! Hope the devs will fix the mousebinding soon –  Oct 14 '19 at 15:11