Datagrid虚拟化会影响项目的选择

时间:2016-02-16 03:41:15

标签: c# wpf datagrid multi-select ui-virtualization

我有一个显示大量数据的数据网格,因此我打开了虚拟化。在我的网格上,我还有一个复选框列,它在我的viewmodel的IsSelected属性上双向绑定。当我尝试全选时,所有项目都被选中。当我选择一行时,该行是唯一在可见区域上选择的行。但是当我向下滚动时,其他项目仍然被选中。所需的行为应该是所选行应该是网格上唯一选定的项目。我认为虚拟化无法处理这种情况。我该如何解决这个问题?

这是我的代码:

查看

<Window x:Class="TestSelection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:testSelection="clr-namespace:TestSelection"
        Title="MainWindow" Height="500" Width="200">
    <Window.DataContext>
        <testSelection:ViewModel/>
    </Window.DataContext>
    <Window.Resources>
        <DataTemplate x:Key="CheckBoxRowHeaderCell">
            <Grid>
                <CheckBox IsChecked="{Binding Path=DataContext.IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                            RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <DataGrid ItemsSource="{Binding Items}"
            RowHeight="20" MaxHeight="500" Width="Auto" ColumnWidth="*"
            AutoGenerateColumns="False" IsReadOnly="True"
            SelectionMode="Extended" SelectionUnit="FullRow"
            CanUserResizeRows="False"
            HeadersVisibility="All"
            EnableRowVirtualization="True"
            VirtualizingStackPanel.VirtualizationMode ="Standard"
            VirtualizingStackPanel.IsVirtualizing ="True"
            RowHeaderTemplate="{DynamicResource CheckBoxRowHeaderCell}">
            <DataGrid.ItemContainerStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                </Style>
            </DataGrid.ItemContainerStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            </DataGrid.Columns>
    </DataGrid>
</Window>

模型

public class SampleItem : INotifyPropertyChanged
{

    private bool p1;
    private string p2;

    public SampleItem(bool p1, string p2)
    {
        IsSelected = p1;
        Name = p2;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaiseNotifyPropertyChanged(string property)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(property));
        }
    }

    private bool isSelected = false;
    /// <summary>
    /// Sets and gets the IsSelected property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public bool IsSelected
    {
        get
        {
            return isSelected;
        }

        set
        {
            if (isSelected == value)
            {
                return;
            }

            isSelected = value;
            RaiseNotifyPropertyChanged("IsSelected");
        }
    }

    private string name = string.Empty;
    /// <summary>
    /// Sets and gets the Name property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public string Name
    {
        get
        {
            return name;
        }

        set
        {
            if (name == value)
            {
                return;
            }

            name = value;
            RaiseNotifyPropertyChanged("Name");
        }
    }
}

视图模型

public class ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<SampleItem> Items
    {
        get
        {
            return new ObservableCollection<SampleItem>()
            {
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
                new SampleItem(true, "4"), new SampleItem(true, "5"), new SampleItem(true, "6"), new SampleItem(true, "7"),
                new SampleItem(true, "1"), new SampleItem(true, "2"), new SampleItem(true, "3"), new SampleItem(true, "4"),
            };
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

0 个答案:

没有答案