lundi 11 mai 2015

Checkbox column flickering when scrolling

I want to make a simple table, whit a checkbox column. I created the table as DataGrid and bound it to a List of custom objects.

Everything works fine, except I notice strange flickering effect, when I scroll the table.
It looks like this: http://ift.tt/1J5ufg3

What's the problem? How can I get rid of this?

My xaml code:

<Window x:Class="MainWindow"
xmlns="http://ift.tt/o66D3f"
xmlns:x="http://ift.tt/mPTqtT"
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>

And vb code:

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

List item I used:

<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

Aucun commentaire:

Enregistrer un commentaire