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.