Silverlight树视图控件选择最后一个节点并查找父节点



如果我创建了一个silverlight应用程序,并使用带有HierarchicalDataTemplate的Treeview控件,该控件有一个父节点,然后是父节点下的子节点,然后又是第一个子节点下的另一个子节点。如果我单击了最后一个子节点,我如何才能将路径返回到父节点?

  •   Child1
      Child2
             ChildA
      Child3
    

所以,如果我有这个树视图,然后点击"ChildA",有没有办法显示路径是Parent-Child2-ChildA感谢

马特。。。我相信,如果你有适当的亲子关系(正如你在问题中提到的那样)&然后使用递归调用,无论何时使用单击树的任何节点,都应该达到您的目的。你可以试试下面的逻辑。

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中显示父节点到叶节点的链接。

最新更新