I have a Window with two use controls. One encapsulates a TreeView control and another is marely a string representation of a selected TreeViewItems details.
TreeView controls is already self sufficient - it will populate itself with content items. Each TreeViewItem has a datacontext.
Here is what I need to happen:
Is my approach correct, in respect to handling the Selected events? How can I get the property value from my selected items data context?
Thank you.
Binding a property to the SelectedItem of TreeView can fix your issue. The code below express what I'm considering. For the two UC you have, you can expose one property for the SelectedItem.
XAML:
<TreeView x:Name="Tree1">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<TextBlock Text="{Binding ElementName=Tree1,Path=SelectedItem.Name}" />
Entity Of Item
public class Item
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
private Collection<Item> items;
public Collection<Item> Items { get { return items; } }
public Item()
{
items = new Collection<Item>();
}
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Binding Source
Item item = new Item { Name = "A" };
item.Items.Add(new Item { Name = "A_1" });
item.Items.Add(new Item { Name = "A_2" });
item.Items.Add(new Item { Name = "A_3" });
Tree1.ItemsSource = new Collection<Item>() { item };