WPF DataGrid 中的 IsSelected 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14918602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:34:09  来源:igfitidea点击:

IsSelected Binding in WPF DataGrid

wpfmvvmbindingdatagrid

提问by Hodaya Shalom

I have in my Model (Class X) Boolean property: IsSelected, is linked to a WPF DataGridas follows:

我在我的模型(X 类)布尔属性中:IsSelected, 链接到 aWPF DataGrid如下:

<DataGrid  SelectedIndex="{Binding SelectedXIndex,Mode=TwoWay}" 
           DataContext="{Binding MyViewModel}" 
           ItemsSource="{Binding ListX}" AutoGenerateColumns="False">
     <DataGrid.RowStyle>
         <Style TargetType="{x:Type DataGridRow}">
             <Setter Property="IsSelected" 
                     Value="{Binding IsSelected, Mode=TwoWay, 
                             UpdateSourceTrigger=PropertyChanged}"/>
         </Style>
     </DataGrid.RowStyle>
</DataGrid>

ListX- ObservableCollection

ListX- 可观察的集合

IsSelecte- Call to NotifyPropertyChange

IsSelecte- 调用 NotifyPropertyChange

It works great.

它工作得很好。

But when I have a lot of rows, that I need to scroll to see them, and I press the button "Select All" that runs the following function, he chooses me only some of the rows and not all: (Even though all the IsSelected on the list is true)

但是当我有很多行时,我需要滚动才能看到它们,然后我按下运行以下功能的“全选”按钮,他只选择了一些行而不是所有行:(即使所有的IsSelected名单上是真实的

public void SelectAll()
{
    ListX.All(c => c.IsSelected = true);
}

I can not understand why this is happening?

我不明白为什么会这样?

采纳答案by Hodaya Shalom

that's what helped me finally:

这就是最终帮助我的原因:

I put in the DataGrid:

我放入数据网格:

VirtualizingStackPanel.VirtualizationMode="Standard"