开发者

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. 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:

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:

<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

If the target is an ItemsControl, the current item is synchronized with the selected item

Any idea on why is this not working?

Thanks in advance.


Even when the documentation says that this will work with any ItemControl what I've read (and seen) is that it only works with 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; set; } In a XAML bind the tree view control SelectedItem property with your SelectedItem property.

Should work.

About SelectedItem -> http://msdn.microsoft.com/en-us/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)
  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
  4. Mark the item that was just selected as IsSelected
  5. Then use custom logic (by custom I mean 150 plus lines of code) to determine where the next item should be. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜