如何访问列表框内的复选框? [英] How to access a Checkbox inside Listbox?

查看:76
本文介绍了如何访问列表框内的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,并且设置了itemstemplate,如下所示.

I have a listbox and I have set the itemstemplate as shown below.

XAML:

            <ListBox ItemsSource="{Binding DataList}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                             <CheckBox x:Name="CheckBox"   HorizontalAlignment="Center" VerticalAlignment="Center" />
                            <TextBlock x:Name="TextBlock" Text="{Binding Title}"  HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,10" FontSize="26.667" TextWrapping="Wrap"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                </ListBox>

我想获取所有选中的复选框.是否有任何方法可以获取每个项目的复选框控件,以便可以检查其 IsChecked 属性.

I want to get which all check box has been selected.Is there any way to get checkbox control for each item so that I can check its IsChecked property.

我可以想到一种绑定IsChecked属性的方法,但是还有其他方法吗?

I can think of a way of binding the IsChecked property.But Is there any other way to do it?

推荐答案

是.一种方法是绑定IsChecked属性.而且,如果您使用的是MVVM,则可能是正确的方法.

Yes. One way to do is to bind the IsChecked property. And if you are using MVVM, probably that's the right way to do it.

无论如何,如果您不想采用绑定方式,并假设要遍历列表框的所有项目并准备已检查项目的列表,请查看是否有帮助: WPF-从DataTemplate查找控件在WPF中

Anyways, if you don't want to go the binding way, and assuming you want to iterate over all items of listbox, and prepare a list of checked items, see if this helps: WPF - Find a Control from DataTemplate in WPF

这篇关于如何访问列表框内的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆