滚动时复选框列闪烁

Checkbox column flickering when scrolling

我想制作一个简单的 table,带有一个复选框列。 我将 table 创建为 DataGrid 并将其绑定到自定义对象的 List

一切正常,除了我注意到滚动 table 时出现奇怪的闪烁效果。
看起来像这样:https://imgrush.com/-jI2FpNF385O

有什么问题吗?我怎样才能摆脱这个?

我的xaml代码:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="723.251">

<Grid>
    <DataGrid x:Name="MainDataGrid" Margin="22,21,133,58" 
              AutoGenerateColumns="False"
              SelectionMode="Single"
              SelectionUnit="Cell"
              CanUserSortColumns="False"
              IsReadOnly="True"
              >
    </DataGrid>
    <Button Content="Populate" HorizontalAlignment="Left" Margin="592,21,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" IsDefault="True"/>
    <Label x:Name="Lbl1" Content="Label" HorizontalAlignment="Left" Margin="592,48,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>

和vb代码:

Imports CADnet_FileReader.CADnet_FileReader

Class MainWindow

Dim OutList As New List(Of Tags)
Dim SelectionLock As New Boolean

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim TableColumn_01 As New DataGridTextColumn
    TableColumn_01.Binding = New Binding("TagItem")
    TableColumn_01.Header = "Tag Name"
    TableColumn_01.Width = 200
    Me.MainDataGrid.Columns.Add(TableColumn_01)

    Dim TableColumn_02 As New DataGridCheckBoxColumn

    TableColumn_02.Binding = New Binding("TagCheck")
    TableColumn_02.Header = "Toogle"
    TableColumn_02.Width = 30

    Me.MainDataGrid.Columns.Add(TableColumn_02)

    Dim TempList As New List(Of String)
    Dim path As String = "C:\Epic\Apps\ElementCounter\Epic_Template.txt"
    TempList = ReadTemplateFile(path)

    Dim ThisItem As New Tags

    For i = 0 To TempList.Count - 1
        ThisItem = New Tags
        ThisItem.TagItem = TempList.Item(i)
        ThisItem.TagCheck = False
        OutList.Add(ThisItem)
    Next

    MainDataGrid.ItemsSource = OutList
End Sub

Private Sub MainDataGrid_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles MainDataGrid.SelectionChanged
End Sub

Private Sub MainDataGrid_SelectedCellsChanged(sender As Object, e As SelectedCellsChangedEventArgs) Handles MainDataGrid.SelectedCellsChanged
    Dim SelectedRow As Integer
    Dim SelectedColumn As Integer

    SelectedRow = MainDataGrid.Items.IndexOf(MainDataGrid.CurrentItem)
    SelectedColumn = MainDataGrid.SelectedCells.Item(0).Column.DisplayIndex

    Lbl1.Content = "Selected Row = " & SelectedRow & "; " & SelectedColumn

    If SelectedColumn = 1 Then
        If OutList.Item(SelectedRow).TagCheck = False Then
            OutList.Item(SelectedRow).TagCheck = True
        Else
            OutList.Item(SelectedRow).TagCheck = False
        End If


    End If

End Sub
End Class

我使用的列表项:

<System.Serializable()> Public Class Tags
    Implements INotifyPropertyChanged
    Public Property TagItem As String
    ' New Property
    Private _NewDataProperty As String
    Public Property TagCheck
        Set(value)
            _NewDataProperty = value
            _PropertyChanged("TagCheck")
        End Set
        Get
            Return _NewDataProperty
        End Get
    End Property
    ' Change events
    Private Sub _PropertyChanged(Optional ByVal PropertyName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
    End Sub
    Private Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Public Sub SerializeMe()
    End Sub
End Class

好吧,问题是,当您滚动到单元格时,单元格就会呈现出来。首先出现复选框,然后出现选择值。 如 smooth scrolling 中所述,您可以尝试设置

ScrollViewer.CanContentScroll=False

这会立即绘制您的整个列表(并停用内容的虚拟化)但是如果您的列表包含很多条目,这可能会成为您性能的真正问题。