【问题标题】:Scrolling issues with checkboxes in a listview (VB / XAML)列表视图中复选框的滚动问题(VB / XAML)
【发布时间】:2016-05-31 12:46:24
【问题描述】:

你好!我在使用 Listview 时遇到问题

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

我没有设法了解解决方案的工作原理,以便将其翻译成 Visual Basic,所以我在这里,希望得到一些帮助!

---------------------- 这是我的程序的工作原理 ---------------- ------

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

->这是我的 Listview 在 XAML 代码中的定义:

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}" />

-> 加载窗口时:

'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

-> 我的 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

-> 结果:

My Listview

-> 这是选中/取消选中复选框时调用的 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

【问题讨论】:

    标签: wpf vb.net xaml listview


    【解决方案1】:

    我设法摆脱了这个错误:检查按钮的状态必须在 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
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多