如果我创建了一个silverlight应用程序并使用了一个带有父节点的HierarchicalDataTemplate的Treeview控件,那么父节点下的子节点就是第一个子节点下的另一个子节点。 如果我点击最后一个子节点,我怎样才能将路径返回到父节点?
父
Child1
Child2
ChildA
Child3
所以,如果我有这个树视图,我点击'ChildA'有一种方法可以显示路径是Parent-Child2-ChildA 谢谢
答案 0 :(得分:0)
马特......我相信如果你有正确的亲子关系(就像你在问题中提到的那样)&然后使用递归调用,只要使用单击树的任何节点然后应该满足您的目的。您可以试试下面的逻辑。
用户体验 -<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中显示父节点到叶节点的链接。