WPF的Datagrid将selectedItem = NULL在MVVM [英] WPF Datagrid selecteditem = null in MVVM

查看:1395
本文介绍了WPF的Datagrid将selectedItem = NULL在MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用MVVM模式一个DataGrid工作。问题是,每当我改变其绑定到的SelectedItem为空虚拟机属性,视图不取消选择当前选择的项目。这是我在XAML绑定:

I'm trying to work with a datagrid using the MVVM pattern. The problem is that whenever I change the VM property which is binded to SelectedItem to null, the View doesn't "deselect" the currently selected item. This is my binding in xaml:

<DataGrid Grid.Column="0" Grid.Row="0" 
    ItemsSource="{Binding Path=Users}" 
    AutoGenerateColumns="False" 
    CanUserAddRows="False" 
    IsReadOnly="True" 
    SelectedItem="{Binding Path=SelectedUser, Mode=TwoWay}">

则selectedItem在SelectedUser财产我总是有选择的对象绑定从视图作品到VM这样。问题是,在虚拟机中我做一些东西,有时会改变SelectedUser属性设置为null所以我期望数据网格取消选择该行也是如此。相反,它保持选中状态,如果我尝试点击同一行,属性不更新。如果我点击任何其他行,属性的变化预期。

The SelectedItem binding works from the view to the VM thus in the SelectedUser property I always have the selected object. The problem is that in the VM I'm doing some stuff which sometimes changes the SelectedUser property to null so I would expect the datagrid to deselect the row as well. Instead, it remains selected and if I try to click on the same row, the property doesn't update. If I click on any other row, the property changes as expected.

有没有一种方法,使DataGrid中取消选择,如果是绑定属性被设置为空?我也正在寻找一个MVVM解决方案,我不想写code后面。我可以写code的后面,所以不要浪费时间,提供这样的解决方案解决这个问题:)

Is there a way to make the datagrid deselect if it's binded property is set to null? Also I'm looking for a MVVM solution as I don't want to write code behind. I can solve this by writing code behind so don't waste time offering such solutions :)

l.e:这是我在虚拟机属性:

l.e.: this is my property in the VM:

public RPLUser SelectedUser
        {
            get
            {                
                return selectedUser;
            }
            set
            {
                selectedUser = value;
                OnPropertyChanged("SelectedUser");
            }
        }

在此先感谢!

推荐答案

我建议检查输出窗口在Visual Studio中,看看是否有结合失败。

I recommend to check the Output Window in visual studio and see if any Binding is failing.

您确信当你选择的东西,选择更新到 SelectedUser 属性?

Are you sure when you select something, the selection updates into the SelectedUser property?

难道ü把一个断点 SelectedUser 的setter和看到当您选择数据网格的东西,它是打?

Did u put a breakpoint in setter of SelectedUser and see that it is hitting when you select something on the datagrid?

对于这种情况的原因绑定来打破可能很多...

The reasons for this Binding to break could be many ...


  1. SelectedUser 是不同类型的不是单个用户的。

  2. SelectedUser 不通过与用户的任何项目引用匹配。

  3. 如何和你在哪里设置空?

  1. SelectedUser is of different type than individual Users.
  2. SelectedUser does not match by reference with any items in Users.
  3. How and where are you setting null?

在我的情况下code工作完全正常...

The following code in my case works perfectly fine...

    <tk:DataGrid MaxHeight="200" AutoGenerateColumns="False"
                 ItemsSource="{Binding}"
                 SelectedItem="{Binding MySelItem,
                                        ElementName=MyDGSampleWindow,
                                        Mode=TwoWay}"
                 IsReadOnly="True">
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Header="Key"
                                   Binding="{Binding Key,
                                                     Mode=OneWay}"/>
            <tk:DataGridTextColumn Header="Value"
                                   Binding="{Binding Value,
                                                     Mode=OneWay}"/>
        </tk:DataGrid.Columns>
    </tk:DataGrid>

当我设置 MyDGSampleWindow.MySelItem 为空,则DataGrid propertly取消选择。也许你会需要给我们你是如何实际设置了一个空值更多的投入。

When I set MyDGSampleWindow.MySelItem as null, the datagrid propertly deselects. Perhaps you might need to give us more input on how are you actually setting the value as null.

这篇关于WPF的Datagrid将selectedItem = NULL在MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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