Hello
I would approach this by creating a control that inherits TreeView, and has the SelectedItem property with a dependency property so it can be used in XAML code and bindings. Let's call the control MyTreeView.
NOTE: By doing this, I don't kow if the property can be SET, but it can certainly be used to get the selected item.
Here's some example code for the control:
public class MyTreeView : TreeView, INotifyPropertyChanged
{
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItem", typeof(Object), typeof(MyTreeView), new PropertyMetadata(null));
public new Object SelectedItem
{
get { return (Object)GetValue(SelectedItemProperty); }
set
{
SetValue(SelectedItemsProperty, value);
NotifyPropertyChanged("SelectedItem");
}
}
public MyTreeView()
: base()
{
base.SelectedItemChanged += new RoutedPropertyChangedEventHandler<Object>(MyTreeView_SelectedItemChanged);
}
private void MyTreeView_SelectedItemChanged(Object sender, RoutedPropertyChangedEventArgs<Object> e)
{
this.SelectedItem = base.SelectedItem;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String aPropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(aPropertyName));
}
}
You can now use your control as an ordinary TreeView control, with your alterations.
<grid>
<local:mytreeview itemssource="{Binding Path=Items, Mode=OneWay}" xmlns:local="#unknown">
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"
Margin="0,0,0,20" />
<textblock text="{Binding Path=SelectedItem, Mode=OneWay}">
VerticalAlignment="Bottom" />
</textblock></local:mytreeview></grid>
Hope it helps!