在 WPF ListView 中以编程方式选择项目 [英] Select item programmatically in WPF ListView

查看:29
本文介绍了在 WPF ListView 中以编程方式选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在 ListView 中以编程方式选择项目.

I'm unable to figure out how to select an item programmatically in a ListView.

我正在尝试使用列表视图的 ItemContainerGenerator,但它似乎不起作用.比如如下操作后obj为null:

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="....ResourcesArrow_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?

推荐答案

ListViewItemIsSelected 属性绑定到模型上的一个属性.然后,您只需使用您的模型,而不必担心 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 中以编程方式选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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