如何获取与列表视图数据模板中存在的复选框关联的值?

我正在尝试获取“选定”复选框的值/内容,以便我可以使用它从后端的 sqlite 数据库获取数据。但我无法从列表视图中获取复选框的值。


这是列表视图 -


<ListView x:Name="listview" Background="Azure" SelectionMode="Multiple"

         ItemsSource="{Binding Strings}" RenderTransformOrigin="0.5,0.5" Width="343">

    <ListView.ItemTemplate>

        <DataTemplate x:Name="datatemplate">

            <CheckBox x:Name="CheckBoxZone" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"

                        Content="{Binding que_text}" Margin="0,5,0,0"/>

        </DataTemplate>

    </ListView.ItemTemplate>

</ListView>

<Button  Style="{StaticResource ResourceKey=button_style }" x:Name="addToSurvey" Content="Add" Click="AddToSurvey_Click" />

这是函数-


private void AddToSurvey_Click(object sender, RoutedEventArgs e)

{


    //foreach (var item in listview.SelectedItems)

    for (int i=0;i< listview.SelectedItems.Count;i++)

    {

        string que = listview.Items[i].ToString(); //did not work

    }


}

这是问题课 -


public class Questions

{

    public int que_id { get; set; }

    public string que_text { get; set; }

}

该复选框包含 que_text 值,我需要检索该值以从数据库获取 que_id。


蝴蝶不菲
浏览 80回答 1
1回答

holdtom

在 WPF 中,我们使用所谓的 MVVM。我们不会像在 winforms 中那样去查看 ListViewItem 控件,而是将所需信息的属性放在视图模型类上,并且使用绑定来告诉 UI 如何更新视图模型类,反之亦然。因此,我们将向 Question 添加 IsSelected 属性。我们还将将该类从Questions重命名为Question,问题集合现在将命名Questions为Strings。public class Question{&nbsp; &nbsp; public int ID { get; set; }&nbsp; &nbsp; public string Text { get; set; }&nbsp; &nbsp; public bool IsSelected { get; set; }}这是您的列表视图。我们将 CheckBox.IsSelected 绑定到 ListViewItem.IsSelected,因此用户只需单击项目上的任意位置即可检查它们。然后我们将 Question.IsSelected 绑定到 ItemContainerStyle 中的 ListViewItem.IsSelected。<ListView&nbsp;&nbsp; &nbsp; x:Name="listview"&nbsp;&nbsp; &nbsp; Background="Azure"&nbsp;&nbsp; &nbsp; SelectionMode="Multiple"&nbsp; &nbsp; ItemsSource="{Binding Questions}"&nbsp;&nbsp; &nbsp; >&nbsp; &nbsp; <ListView.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CheckBox&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content="{Binding Text}" Margin="0,5,0,0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; </ListView.ItemTemplate>&nbsp; &nbsp; <ListView.ItemContainerStyle>&nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="ListViewItem">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="IsSelected" Value="{Binding IsSelected}" />&nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; </ListView.ItemContainerStyle></ListView>以下是我们如何处理该事件处理程序中选定的问题。我猜测您的Strings收藏是 Window 或您拥有的任何视图的成员;如果情况并非如此,请告诉我,我们将找出解决办法。请记住,我们Questions现在调用该集合。private void AddToSurvey_Click(object sender, RoutedEventArgs e){&nbsp; &nbsp; string allQuestionsText = "";&nbsp; &nbsp; foreach (var question in Questions.Where(q => q.IsSelected))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; I don't know what you really want to do in here, but it sounds like you do.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; allQuestionsText += question.Text + "\n";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP