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 :
-> 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