WPF DataGrid - 虚拟化 - 全选

时间:2013-09-21 19:09:43

标签: wpf datagrid datatable

使用WPF虚拟化DataGrid,我需要使用'全选'方法。迭代虽然网格本身不起作用,因为它只会选择UI中可见的记录 - 当然,如果使用鼠标选择它们,它们将被保留。我猜测DataTable必须改变一些东西,但是什么呢?如果是这样,它可以被操纵,以便在DataGrid中选择所有记录吗?

由于

======================== ADDED ====================== =====

伊兰 - 感谢您的回复,但有一份工作接线

  1. 创建DataGrid

     Dim DGV As New CustomControl.DGVx
        With DGV
            .Name = "Invoice_AdHoc_DGV"
            .AutoGenerateColumns = False
            .SelectionMode = SelectionMode.Multiple
        End With
        RegisterControl(Invoice_AdHoc_Grid, DGV)
        RightGrid.Children.Add(DGV)
    
  2. 绑定到DataTable

    DGV.ItemsSource = AdHocDT.DefaultView
    
  3. 根据您的回答创建课程

    Public Class ObjectSelectAll
    Implements System.ComponentModel.INotifyPropertyChanged
    Public Property Name() As String
    Private vIsSelected As Boolean
    Public Property IsSelected() As Boolean
    Get
        Return vIsSelected
    End Get
    Set(value As Boolean)
        vIsSelected = value
        RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("IsSelected"))
    End Set
    End Property
    
    
    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    

    结束班

2 个答案:

答案 0 :(得分:0)

cs:

    private List<MyObject> _items;
    public List<MyObject> Items
    {
        get
        {
            if(_items == null)
                _items = new List<MyObject> { new MyObject { Name = "obj1" }, new MyObject { Name = "obj2" }, new MyObject { Name = "obj3" }, new MyObject { Name = "obj4" } };
            return _items;
        }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        foreach (var item in Items)
        {
            item.IsSelected = true;
        }
    }
}

public class MyObject : INotifyPropertyChanged
{
    public string Name { get; set; }

    private bool _isSelected;
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

xaml:

    <Button Click="Button_Click_1" Content="Select All"/>

    <DataGrid Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>                    
            </Style>
        </DataGrid.RowStyle>

        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>    
        </DataGrid.Columns>

    </DataGrid>

答案 1 :(得分:0)

最后设法解决了这个问题

在DataTable中添加了另一列

 With AdHocDT.Columns
                 .Add("Selected", GetType(Boolean))
            End With

当选择全部&#39;单击将所有行设置为True,并在网格中将值更新为true,然后添加此项以确保网格在虚拟数据滚动到视图时将其显示为选中

Private Sub Invoice_AdHoc_DGV_ItemsChanged(sender As Object, e As EventArgs)
    Try
        Dim DGV As CustomControl.DGVx = Invoice_AdHoc_Grid.FindName("Invoice_AdHoc_DGV")
        Dim i As Integer = 0
         For Each Row As DataRowView In DGV.Items
            Dim vRow As DevComponents.WPF.Controls.AdvGridRow = CType(DGV.ItemContainerManager.ContainerFromItem(DGV.Items(i), False), DevComponents.WPF.Controls.AdvGridRow)
            If Not vRow Is Nothing Then
                If Row("Selected") = True Then
                    vRow.IsSelected = True
                End If
            End If
            i += 1
        Next

    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

并处理事件

AddHandler DGV.ItemContainerManager.VisibleContainersChanged, AddressOf Invoice_AdHoc_DGV_ItemsChanged

现在网格显示所有选定的行: - )