wpf 以编程方式选择列表框中的项目/索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/831296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically selecting Items/Indexes in a ListBox
提问by Alex
In WPF, I'd like to set the selected indexes of a System.Windows.Controls.ListBox
在 WPF 中,我想设置 System.Windows.Controls.ListBox 的选定索引
I best way I've found so far is to remove all the items from the control, insert the selected, call SelectAll(), then insert the rest, but this solution neither works in my situation nor is very efficient.
到目前为止,我发现的最好方法是从控件中删除所有项目,插入所选项目,调用 SelectAll(),然后插入其余项目,但此解决方案在我的情况下既不适用,也不是很有效。
So, how do you set items in a Listbox to be selected, programmatically?
那么,如何以编程方式设置要选择的 Listbox 中的项目?
采纳答案by mdm20
One way you can do this is to add a Selected field to your data object. Then you need to overide the default listboxitem style and bind the isselected property to the Selected property in your object. Then you just need to go through your data items and update the Selected value.
您可以这样做的一种方法是将 Selected 字段添加到您的数据对象。然后你需要覆盖默认的 listboxitem 样式并将 isselected 属性绑定到对象中的 Selected 属性。然后您只需要浏览您的数据项并更新 Selected 值。
If you don't implement that Selected property as a dependency property, you need your class to implented the INotifyPropertyChanged interface and raise the propertychanged event when you set the value.
如果您不将该 Selected 属性实现为依赖属性,则需要您的类实现 INotifyPropertyChanged 接口并在设置值时引发 propertychanged 事件。
回答by danlash
You can set multiple items as selected by using the SelectedItems collection. This isn't by index, but by what you have bound:
您可以使用 SelectedItems 集合将多个项目设置为选中状态。这不是按索引,而是按您所绑定的:
foreach (var boundObject in objectsBoundToListBox)
{
ListBox.SelectedItems.Add(boundObject);
}
回答by jgallant
You have to do this:
你必须这样做:
ListBoxObject.SelectedItem = ListBoxObject.Items.GetItemAt(itemIndex);
Where itemIndex would be the item you want to select. If you want to select multiple items, you need to use the ListBox.SelectedIndexCollection property.
其中 itemIndex 将是您要选择的项目。如果要选择多个项目,则需要使用 ListBox.SelectedIndexCollection 属性。
回答by jgallant
how to programmatically select multiple items in listbox in wpf
如何以编程方式在 wpf 的列表框中选择多个项目
foreach (var boundObject in objectsBoundToListBox)
{
ListBox.SelectedItems.Add(boundObject);
}
回答by Chathuranga Wijeratna
Thanks to mdm20.
My case was actually checking a CheckBox
within the ListBox
, and this Dependency Property worked like a charm.
I had to inherit my custom class from DependencyObject
and implement the property
感谢 mdm20。我的案例实际上是CheckBox
在 中检查 a ListBox
,而这个依赖属性就像一个魅力。我必须继承我的自定义类DependencyObject
并实现该属性
public class ProjectListItem : DependencyObject{
public Boolean IsChecked
{
get { return (Boolean)this.GetValue(CheckedProperty); }
set { this.SetValue(CheckedProperty, value); }
}
public static readonly DependencyProperty CheckedProperty =
DependencyProperty.Register("IsChecked", typeof(Boolean), typeof(ProjectListItem),
new PropertyMetadata(false));
}
回答by user3878444
You can do this for multiple sections:
您可以为多个部分执行此操作:
ListBoxObject.SelectedItems.Add(ListBoxObject.Items.GetItemAt(i));
Where i is the item index.
其中 i 是项目索引。