列表框-获取所选项目的列表(WPF UserControl& C#) [英] ListBox - Get List of Selected Items (WPF UserControl & C#)

查看:100
本文介绍了列表框-获取所选项目的列表(WPF UserControl& C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是WPF和C#的新手.我试图在网络上寻找有关如何捕获列表框中所有选定项目的列表的简单教程或代码.

我正在执行以下操作:

1.具有列表< ABC>.其中A具有两个属性:名称和地址.
2.我正在使用DataBinding通过以下方式填充ListBox:

XAML:

< ListView名称=" AddData " ItemsSource ="{Binding}" DisplayMemberPath =名称"/>

C#:

AddData .DataContext = XClass.getMyABCList(); //这将返回List< ABC>

当我在ListBox中看到Name属性值时,所有这些工作正常.

现在,我要选择列表中的3个项目.当我单击完成"按钮时,我想获得以下信息:

1)所选项目的索引
2)所选项目的名称值
3)所选项目的地址值.

我可以得到(2)和(3),如果我可以得到(1),因为我可以交叉引用主列表< abc>

希望对此有所帮助.

谢谢,

Manish

Hi,

I''m new to WPF and C#. I''ve tried to seach the web for a simple tutorial or code on how I can capture the list of all the selected items in a listbox.

I am doing the following:

1. Have a List<ABC> where A has two properties: Name and Address.
2. I am using DataBinding to populate a ListBox by the following:

XAML:

<ListView Name="AddData" ItemsSource="{Binding}" DisplayMemberPath="Name" />

C#:

AddData.DataContext = XClass.getMyABCList(); // This returns the List<ABC>

This all works as I see the the Name property values in the ListBox.

Now, I want to select 3 of the items in the list. When I click on a "Done" button, I want to get the following information:

1) The indexes of the selected items
2) The Name values of the selected items
3) The Address values of the selected items.

I can obtain (2) and (3) if I can get (1) as I can cross reference the master List<abc>

Would appreciate any help in this.

Thanks,

Manish

推荐答案

与其尝试以Win Forms的方式进行操作,还可以通过以WPF为中心的方法向该类添加IsSelected 属性来实现此目的.在列表的后面.然后,您可以使用以下方法绑定到该属性:
Rather than attempting to do this in a Win Forms manner, you can achieve this in a WPF-centric way by adding an IsSelected property to the class behind the list. Then you bind to this property using the following:
<ListBox.ItemContainerStyle>
  <Style TargetType="{x:Type ListBoxItem}">
     <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
  </Style>
</ListBox.ItemContainerStyle>

现在您的基础列表将适当地设置了IsSelected boolean,并且您只需选择设置了该值的项目即可.

Now your underlying list will have the IsSelected boolean set as appropriate, and you can just select the items where that value is set.


这篇关于列表框-获取所选项目的列表(WPF UserControl&amp; C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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