A. Yotsema A. Yotsema - 6 months ago 37
Vb.net Question

Scrolling issues with checkboxes in a listview (VB / XAML)

Hello ! I'm having trouble with a Listview full of Checkboxes that check/uncheck themselves when i'm scrolling up and down.

It seems to be a known problem, and I find a lot of topics with solutions, but only for Java/Android applications, as you can see here :
checkbox unchecked when i scroll listview in android

I did not manage to get how the solution works in order to translate it into Visual Basic, so here I am, hoping for some help !

---------------------- Here is how my program work ----------------------

I have a DataGrid with certain number of columns. In my Listview, each checkbox is link to a column, in order to show/hide the column when the checkbox is checked/unchecked.

-> Here is the definition of my Listview in the XAML code :

1) In the window resources :

<DataTemplate x:Key="Check_Template">
<CheckBox Name="checkbox" Content="{Binding sL_Name}" Click="CheckBox_Click_1" IsChecked="True"/>
</DataTemplate>


2) In the window definition :

<ListView x:Name="Colum_Select" ItemTemplate="{StaticResource Check_Template}" />


-> When the window is loaded :

'Creation of a list which will get the headers of the datagrid columns

Dim L_View As New List(Of L_class)
For Each prop In MyDataGrid.Columns
L_View.Add(New L_class(prop.Header.ToString))
Next

'Send them to the Listview in my XAML code

Colum_Select.ItemsSource = L_View


-> The definition of my L_class which will get my column headers :

Class L_class

Public L_Name As String

Sub New(One As String)
L_Name = One
End Sub

Public Property sL_Name() As String
Get
Return L_Name
End Get
Set(value As String)
L_Name = value
End Set
End Property

End Class


-> The result :

My Listview

-> Here is the VB fonction called when a checkbox is check/unchecked :

Private Sub CheckBox_Click_1(sender As Object, e As RoutedEventArgs)

'Get the checkbox which fires the event
Dim senderCB As CheckBox = sender

'Find the column in my datagrid that correspond with the checkbox
Dim item As Object = FindName(senderCB.Content.ToString)
Dim col As DataGridColumn = item

'Hide/Show this column
If (senderCB.IsChecked = True) Then
col.Visibility = Visibility.Visible
Else
col.Visibility = Visibility.Collapsed
End If

End Sub

Answer

I managed to get rid of this bug : the state of the checkbutton must be define in the VB code, not in the XAML ! Here is how i proceeded.

In the window ressources, I binded the IsChecked property ...

<DataTemplate x:Key="Check_Template">
        <CheckBox Name="checkbox" Content="{Binding sL_Name}" Click="CheckBox_Click_1" IsChecked="{Binding sL_Check}" />
</DataTemplate>

... to the correspondant property in my L_Class ...

Class L_class

Public L_Name As String
Public L_Check As Boolean

Sub New(One As String, Two As Boolean)
    L_Name = One
    L_Check = Two
End Sub

Public Property sL_Name() As String
    Get
        Return L_Name
    End Get
    Set(value As String)
        L_Name = value
    End Set
End Property

Public Property sL_Check() As Boolean
    Get
        Return L_Check
    End Get
    Set(value As Boolean)
        L_Check = value
    End Set
End Property

End Class

... and then adapt the code when the window load :

    Dim L_View As New List(Of L_class)

    For Each prop In ResultatSQLDataGrid.Columns
        L_View.Add(New L_class(prop.Header.ToString, True))
    Next

    Colum_Select.ItemsSource = L_View

And no more auto-checking when scrolling in my case.

Hoping my monologue will be of some help. Cheers !

Comments