0

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

谢谢

=========================添加========================= ==

Eran - 感谢您的回复,但有工作将其连接起来

  1. 创建数据网格

     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. 绑定到数据表

    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
    

    结束类

4

2 回答 2

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

xml:

    <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>
于 2013-09-21T20:17:18.643 回答
0

终于设法解决了这个问题

向 DataTable 添加了另一列

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

单击“全选”时,将所有行的此项设置为 True,并且在 Grid 中,该值也更新为 true,然后添加此项以确保当虚拟数据滚动到视图中时 Grid 将其显示为选中状态

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

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

于 2013-09-28T18:27:18.540 回答