简体   繁体   English

ListCollectionView不会在WPF中的TreeView上跟踪当前选定的项目

[英]ListCollectionView does not track the current selected item on a TreeView in WPF

I've done this really simple example, is a Window with a TreeView and a Button. 我已经完成了这个非常简单的示例,即一个带有TreeView和Button的窗口。 When you click the button you should see the selected item, but is not working, the CurrentItem property does not get updated when you change the selection: 当您单击按钮时,您应该看到选定的项目,但是它不起作用,当您更改选择时,CurrentItem属性不会得到更新:

C#: C#:

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;

namespace TreeViewSort
{
public partial class Window1
{
    private ObservableCollection<string> _items;
    public ListCollectionView SortedItems { get; private set; }

    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _items =new ObservableCollection<string>();
        _items.Add("ZZ");
        _items.Add("AA");
        _items.Add("CA");
        _items.Add("DA");
        _items.Add("EA");

        this.SortedItems = new ListCollectionView(_items);
        this.DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(this.SortedItems.CurrentItem.ToString());
    }
}
}

XAML: XAML:

<Window x:Class="TreeViewSort.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <DockPanel>
        <TreeView DockPanel.Dock="Top" Name="treeView1" ItemsSource="{Binding SortedItems, Mode=TwoWay}" MinHeight="200" />
        <Button DockPanel.Dock="Bottom" Click="Button_Click">
            Test
        </Button>
    </DockPanel>
</Window>

The MSDN documentation says MSDN文档说

If the target is an ItemsControl, the current item is synchronized with the selected item 如果目标是ItemsControl,则当前项目与所选项目同步

Any idea on why is this not working? 关于为什么这不起作用的任何想法吗?

Thanks in advance. 提前致谢。

即使文档说这可以与任何ItemControl一起使用,但我已经阅读(看到)的是它仅适用于Selectors ...

I'm not sure about CurrentItem in ListCollectionView, try to do this below: Create a property in your Window1 class -> public string SelectedItem { get; 我不确定ListCollectionView中的CurrentItem,请尝试以下操作:在Window1类中创建一个属性->公共字符串SelectedItem {get; set; 组; } In a XAML bind the tree view control SelectedItem property with your SelectedItem property. 在XAML中,将树视图控件SelectedItem属性与SelectedItem属性绑定。

Should work. 应该管用。

About SelectedItem -> http://msdn.microsoft.com/en-us/library/system.windows.controls.treeview.selecteditem.aspx 关于SelectedItem-> http://msdn.microsoft.com/zh-cn/library/system.windows.controls.treeview.selecteditem.aspx

Regards Pawel Jankowski 问候帕维尔·扬科夫斯基

I had to do this and it wasn't pretty. 我必须这样做,这并不漂亮。 I have a hybrid tree view / data grid. 我有一个混合树视图/数据网格。 To be able to move to the next / previous item or move items up and down I had to do the following. 为了能够移至下一个/上一个项目或上下移动项目,我必须执行以下操作。

  1. Add an IsSelected property to my model (hack - I know) 将IsSelected属性添加到我的模型中(hack-我知道)
  2. Each Model had a sort order property 每个模型都有一个排序顺序属性
  3. When an item was selected in the tree - use linq queries to find any other items that were selected and mark as non selected 在树中选择一个项目时-使用linq查询查找其他所有已选择的项目并将其标记为未选择
  4. Mark the item that was just selected as IsSelected 将刚刚选择的项目标记为IsSelected
  5. Then use custom logic (by custom I mean 150 plus lines of code) to determine where the next item should be. 然后使用自定义逻辑(按我的定义,我的意思是150多行代码)来确定下一个项目应该在哪里。 This logic however was accounting for items that were collapsed in the tree and two levels (parent / child). 但是,此逻辑考虑了在树和两个级别(父级/子级)中折叠的项目。

IIRC the reason I had to do this was the values I needed were on the visual tree but I was working with the logic tree. IIRC之所以这样做,是因为我需要的值在可视树上,但我正在使用逻辑树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM