WPF的ListView以编程方式选择项目 [英] WPF ListView Programmatically Select Item

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

问题描述

哦,伙计。我这周感觉WPF疼痛。所以,我的下一个把戏,我现在无法弄清如何以编程方式在一个ListView选择一个项目。

Oh, man. I'm feeling the WPF pain this week. So for my next trick, I'm now unable to figure out how to select an item programmatically in a ListView.

我在尝试使用列表视图的ItemContainerGenerator,但它只是似乎并没有工作。例如,物镜以下操作之后为空:

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]);
}

这整个事情的关键在于,我只是想$ P $在我的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?

非常感谢您的时间。

推荐答案

IsSelected 属性绑定的的ListViewItem 对模型的属性。然后,你只需要使用你的模型工作,而不是担心用户界面,其中包括围绕容器虚拟化的潜在危害的复杂性。

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天全站免登陆