我无法弄清楚如何以编程方式在ListView中选择项目.
I'm unable to figure out how to select an item programmatically in a ListView.
我正在尝试使用列表视图的ItemContainerGenerator,但似乎不起作用.例如,执行以下操作后,obj为空:
I'm attempting to use the listview's ItemContainerGenerator, but it just doesn't seem to work. For example, obj is null after the following operation:
//VariableList is derived from BindingList m_VariableList = getVariableList(); lstVariable_Selected.ItemsSource = m_VariableList; var obj = lstVariable_Selected.ItemContainerGenerator.ContainerFromItem(m_VariableList[0]);我已经尝试过(根据在这里和其他地方看到的建议)使用ItemContainerGenerator的StatusChanged事件,但无济于事.该事件永远不会触发.例如:
I've tried (based on suggestions seen here and other places) to use the ItemContainerGenerator's StatusChanged event, but to no avail. The event never fires. For example:
m_VariableList = getVariableList(); lstVariable_Selected.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged); lstVariable_Selected.ItemsSource = m_VariableList; ... void ItemContainerGenerator_StatusChanged(object sender, EventArgs e) { //This code never gets called var obj = lstVariable_Selected.ItemContainerGenerator.ContainerFromItem(m_VariableList[0]); }整个问题的症结在于,我只想预选ListView中的一些项目.
The crux of this whole thing is that I simply want to pre-select a few of the items in my ListView.
为了不遗漏任何内容,ListView使用了一些模板化和拖放功能,因此我在此处包括了XAML.本质上,此模板使每个项目成为带有一些文本的文本框-选中任何项目后,都将选中该复选框.而且每个项目下面都有一个小字形以插入新项目(一切正常):
In the interest of not leaving anything out, the ListView uses some templating and Drag/Drop functionality, so I'm including the XAML here. Essentially, this template makes each item a textbox with some text - and when any item is selected, the checkbox is checked. And each item also gets a little glyph underneath it to insert new items (and this all works fine):
<DataTemplate x:Key="ItemDataTemplate_Variable"> <StackPanel> <CheckBox x:Name="checkbox" Content="{Binding Path=ListBoxDisplayName}" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" /> <Image ToolTip="Insert Custom Variable" Source="..\..\Resources\Arrow_Right.gif" HorizontalAlignment="Left" MouseLeftButtonDown="OnInsertCustomVariable" Cursor="Hand" Margin="1, 0, 0, 2" Uid="{Binding Path=CmiOrder}" /> </StackPanel> </DataTemplate> ... <ListView Name="lstVariable_All" MinWidth="300" Margin="5" SelectionMode="Multiple" ItemTemplate="{StaticResource ItemDataTemplate_Variable}" SelectionChanged="lstVariable_All_SelectionChanged" wpfui:DragDropHelper.IsDropTarget="True" wpfui:DragDropHelper.IsDragSource="True" wpfui:DragDropHelper.DragDropTemplate="{StaticResource ItemDataTemplate_Variable}" wpfui:DragDropHelper.ItemDropped="OnItemDropped"/>那我想念什么?如何以编程方式选择ListView中的一个或多个项目?
So what am I missing? How do I programmatically select one or more of the items in the ListView?
推荐答案将ListViewItem的IsSelected属性绑定到模型上的属性.然后,您只需要使用模型即可,而不必担心UI的复杂性,UI包括围绕容器虚拟化的潜在危害.
Bind the IsSelected property of the ListViewItem to a property on your model. Then, you need only work with your model rather than worrying about the intricacies of the UI, which includes potential hazards around container virtualization.
例如:
<ListView> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="IsSelected" Value="{Binding IsGroovy}"/> </Style> </ListView.ItemContainerStyle> </ListView>现在,只需使用模型的IsGroovy属性来选择/取消选择ListView中的项目.
Now, just work with your model's IsGroovy property to select/deselect items in the ListView.
更多推荐
在WPF ListView中以编程方式选择项目
发布评论