【问题标题】:How to select listviewitem programmatically to behave like selecting with mouse/keyboard如何以编程方式选择 listviewitem 的行为就像使用鼠标/键盘进行选择一样
【发布时间】:2012-09-26 02:32:25
【问题描述】:

我正在尝试使用代码模仿 ListView 中选择事件的行为。下面是示例代码:

<StackPanel>
    <Button x:Name="buttonSelectAbove" Content="Select Above"/>
    <Button x:Name="buttonSelectBelow" Content="Select Below"/>
    <ListView x:Name="listView"/>
</StackPanel>

下面是代码:

void buttonSelectBelow_Click(object sender, RoutedEventArgs e)
{
    listView.SelectedIndex += 1;
    ListViewItem lvi = listView.ItemContainerGenerator.ContainerFromItem(listView.SelectedItem) as ListViewItem;
    if (lvi != null)
    {
        lvi.Focus();
    }
}

void buttonSelectAbove_Click(object sender, RoutedEventArgs e)
{
    listView.SelectedIndex -= 1;
    ListViewItem lvi = listView.ItemContainerGenerator.ContainerFromItem(listView.SelectedItem) as ListViewItem;
    if (lvi != null)
    {
        lvi.Focus();
    }
}

现在,如果您尝试这些代码,单击每个按钮并使用键盘上下移动列表将按预期工作,但是当您使用 SHIFT 单击选择一系列项目时,使用鼠标选择的行为会有所不同,这里有一些图像使其更清晰:

正常选择行为:
点击 C

然后shift点击E(将导致选择c、d、e)

通过代码行为选择:
点击A,

单击下面的按钮选择两次,因此将选择 C ​​

然后shift点击E(会选择a、b、c、d、e)

那么如何通过代码实现正常的选择行为呢?

【问题讨论】:

标签: wpf listview


【解决方案1】:

如果您不喜欢 LIstView 上的 SelectionMode="Single" 可以选择多行。

【讨论】:

  • 我希望它能够多选,只是程序选择后SHIFT单击的行为与正常的不同。
  • 听起来像是重复的情况; stackoverflow.com/questions/9689987/…
  • 啊,是的,没有找到那个,因为我不知道要搜索的正确关键字,看起来“多选锚点”是关键,谢谢你的链接
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 2022-01-23
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多