列表视图中的复选框滚动问题(VB / XAML)

时间:2016-05-31 12:46:24

标签: wpf vb.net xaml listview

您好!我在使用 Listview 完整的 Checkboxes 时出现问题,当我上下滚动时检查/取消选中它们。

这似乎是一个已知问题,我发现很多主题都有解决方案,但仅适用于 Java / Android应用程序,您可以在此处看到: checkbox unchecked when i scroll listview in android

我无法了解解决方案的工作原理,以便将其转换为Visual Basic,所以我希望能提供一些帮助!

----------------------这是我的计划如何运作---------------- ------

我有一个包含一定数量列的DataGrid。在我的Listview中,每个复选框都链接到一列,以便在选中/取消选中复选框时显示/隐藏列。

- >以下是XAML代码中Listview的定义:

1)在窗口资源中

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

2)在窗口定义中

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

- &GT;加载窗口时:

'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

- &GT;我的L_class的定义将获取我的列标题:

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

- &GT;结果:

My Listview

- &GT;这是检查/取消选中复选框时调用的VB函数:

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

1 个答案:

答案 0 :(得分:0)

我设法摆脱了这个错误:检查按钮的状态必须在VB代码中定义,而不是在XAML中定义!以下是我的进展情况。

在窗口资源中,我绑定了IsChecked属性...

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

...到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

...然后在窗口加载时调整代码:

    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

在我的情况下滚动时不再自动检查。

希望我的独白会有所帮助。干杯!