首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在WPF ListView中以编程方式选择项

在WPF ListView中以编程方式选择项
EN

Stack Overflow用户
提问于 2009-07-01 14:49:58
回答 4查看 54.9K关注 0票数 22

我不知道如何在ListView中以编程方式选择项。

我正在尝试使用列表视图的ItemContainerGenerator,但它似乎就是不起作用。例如,执行以下操作后,obj为null:

代码语言:javascript
复制
//VariableList is derived from BindingList
m_VariableList = getVariableList();
lstVariable_Selected.ItemsSource = m_VariableList;
var obj = 
    lstVariable_Selected.ItemContainerGenerator.ContainerFromItem(m_VariableList[0]);

我已经尝试(根据在这里和其他地方看到的建议)使用ItemContainerGenerator的StatusChanged事件,但是没有用。该事件永远不会触发。例如:

代码语言:javascript
复制
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中预先选择几个项目。

为了不遗漏任何东西,ListView使用了一些模板和拖放功能,所以我在这里包含了XAML。本质上,这个模板使每个项目都是一个包含一些文本的文本框-当任何项目被选中时,复选框就会被选中。并且每个条目下面都有一个小的字形来插入新的条目(这一切都很好用):

代码语言:javascript
复制
<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中的一个或多个项?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-07-01 15:12:33

ListViewItemIsSelected属性绑定到模型上的属性。然后,您只需要使用您的模型,而不是担心UI的错综复杂,这包括容器虚拟化的潜在风险。

例如:

代码语言:javascript
复制
<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding IsGroovy}"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

现在,只需使用模型的IsGroovy属性来选择/取消选择ListView中的项。

票数 35
EN

Stack Overflow用户

发布于 2015-06-24 20:46:18

其中'this‘是ListView实例。这不仅会更改选择,还会将焦点设置在新选择的项目上。

代码语言:javascript
复制
  private void MoveSelection(int level)
  {
     var newIndex = this.SelectedIndex + level;
     if (newIndex >= 0 && newIndex < this.Items.Count)
     {
        this.SelectedItem = this.Items[newIndex];
        this.UpdateLayout();
        ((ListViewItem)this.ItemContainerGenerator.ContainerFromIndex(newIndex)).Focus();
     }
  }
票数 6
EN

Stack Overflow用户

发布于 2009-07-01 15:06:07

这是我最好的猜测,这将是一种更简单的选择方法。由于我不确定您选择的是什么,下面是一个通用示例:

代码语言:javascript
复制
var indices = new List<int>();

for(int i = 0; i < lstVariable_All.Items.Count; i++)
{
  // If this item meets our selection criteria 
  if( lstVariable_All.Items[i].Text.Contains("foo") )
    indices.Add(i);
}

// Reset the selection and add the new items.
lstVariable_All.SelectedIndices.Clear();

foreach(int index in indices)
{
  lstVariable_All.SelectedIndices.Add(index);
}

我习惯于看到的是一个可设置的SelectedItem,但我看到你不能设置或添加它,但希望这个方法可以作为替代。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1069577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档