Мэтт ... Я полагаю, что если у вас есть правильная ссылка «родитель-потомок» (как та, которую вы упомянули в своем вопросе), а затем использовать рекурсивные вызовы всякий раз, когда вы нажимаете любой узел вашего дерева, это должно служить вашей цели.Вы можете попробовать приведенную ниже логику.
UX -
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<sdk:TreeView x:Name="tvGroups" Grid.Row="0">
<sdk:TreeView.ItemTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
<TextBlock Text="{Binding ItemName}" Tag="{Binding ItemID}" MouseLeftButtonUp="TextBlock_MouseLeftButtonUp" />
</sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>
</sdk:TreeView>
<TextBlock x:Name="txbParentToChild" Grid.Row="1"/>
</Grid>
Код позади -
namespace SilverlightApplication1
{
public class Group : INotifyPropertyChanged
{
private String _strItemName;
private Int32 _itemId;
private ObservableCollection<Group> _subItems;
public String ItemName
{
get { return _strItemName; }
set { _strItemName = value; NotifyChange("ItemName"); }
}
public Int32 ItemID
{
get { return _itemId; }
set { _itemId = value; NotifyChange("ItemID"); }
}
public ObservableCollection<Group> SubItems
{
get { return _subItems; }
set { _subItems = value; NotifyChange("SubItems"); }
}
/// <summary>
/// Called whenever any of the Group property is changed.
/// </summary>
/// <param name="PropertyName">Name of the property that has changed.</param>
private void NotifyChange(String PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public partial class MainPage : UserControl
{
private ObservableCollection<Group> _lstItems;
private Int32 _selectedItemId;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
_lstItems = new ObservableCollection<Group>();
ObservableCollection<Group> childA = new ObservableCollection<Group>();
childA.Add(new Group { ItemID = 1, ItemName = "ChildA", SubItems = null });
ObservableCollection<Group> parent = new ObservableCollection<Group>();
parent.Add(new Group { ItemID = 2, ItemName = "Child1", SubItems = null });
parent.Add(new Group { ItemID = 3, ItemName = "Child2", SubItems = childA });
parent.Add(new Group { ItemID = 4, ItemName = "Child3", SubItems = null });
_lstItems.Add(new Group { ItemID = 5, ItemName = "Parent", SubItems = parent });
tvGroups.ItemsSource = _lstItems;
}
private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
TextBlock txbSource = sender as TextBlock;
String strItems = String.Empty;
if (txbSource != null)
{
_selectedItemId = -1;
Int32.TryParse(txbSource.Tag.ToString(), out _selectedItemId);
List<String> lstParent = new List<String>();
if (_selectedItemId != -1)
{
lstParent = FindItem(_lstItems);
}
lstParent.Reverse();
foreach(String strItem in lstParent)
{
strItems += strItem + " -> ";
}
strItems = strItems.Remove(strItems.Length - 4);
}
txbParentToChild.Text = strItems;
}
private List<String> FindItem(ObservableCollection<Group> lstCurrentGroup)
{
List<String> lstParent = new List<String>();
foreach(Group grp in lstCurrentGroup)
{
if (grp.ItemID == _selectedItemId)
{
lstParent.Add(grp.ItemName);
return lstParent;
}
else if (grp.SubItems != null)
{
lstParent = FindItem(grp.SubItems);
if (lstParent.Count > 0)
lstParent.Add(grp.ItemName);
}
}
return lstParent;
}
}
}
Над кодом отображается ссылка на родительский узел в TextBlock всякий раз, когдапользователь нажимает на узлы дерева.