我想创建一个简单的表格,使用复选框列。
我将表创建为DataGrid
并将其绑定到List
个自定义对象。
当我滚动桌子时,除了我注意到奇怪的闪烁效果外,一切正常。
它看起来像这样: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
答案 0 :(得分:2)
嗯,问题是,在滚动到它们的那一刻,单元格会被渲染。首先出现复选框,然后是选择值。 如smooth scrolling中所述,您可以尝试设置
ScrollViewer.CanContentScroll=False
会立即绘制整个列表(并停用内容的虚拟化)但如果您的列表包含大量条目,这可能会成为您性能的真正问题。