ListView无法与ObservableCollection一起正常使用 [英] ListView isn't working properly with ObservableCollection

查看:48
本文介绍了ListView无法与ObservableCollection一起正常使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处是我正在使用的代码.在 xaml 中,我像这样扩展了 ListView :

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>

并在 Master 类中实现了 INotifyPropertyChanged ,以在编辑项目时在 ListView 中查看更新.在 Person 类中,我又添加了一个 Command

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; }

通过构造将其初始化:

Edit = new Command(CanClick, EditItem);

并实现了这样的回调:

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);
    }
}

当我选择一个项目并点击 Return 时,它会更新集合,但 ListView 中没有任何变化.如果我双击某个项目,则它既不会更新集合,也不会更新 ListView

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!

另一个问题是为什么我必须为 ListView 设置一个名称,以便在嵌套的 InputBindings 中使用,例如 ElementName = lv ?我可以摆脱它吗?

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?

编辑

如果我像这样进一步展开 ListView :

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 反映了我通过 void EditItem(object obj)进行的更新,但 MouseBinding 也不以这种方式工作.为什么一个人将这样的单个属性绑定为一个集合?

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?

推荐答案

没有理由调用 SetItem 来更新项目.

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

只要 Master 类正确实现 INotifyPropertyChanged ,设置属性就足够了.

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

在处理双击时,您可以添加对 System.Windows.Interactivity ,并使用 EventTrigger :

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>

这篇关于ListView无法与ObservableCollection一起正常使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆