从循环列表中查找父类

本文关键字:查找 父类 列表 循环 | 更新日期: 2023-09-27 18:15:34

我有一个像下面这样的类,

public class Folder : BindableBase
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value;  }
    }
    private List<Folder> subfolders=new List<Folder>();
    public List<Folder> Folders
    {
        get { return subfolders; }
        set{ subfolders= value; }
    }
}

该类绑定到一个TreeView HierarchicalDataTemplate当我从TreeView中选择任何节点时,我将获得SelectedItem作为Folder class object。我如何在View Model中找到SelectedItem的父类。

HierarchicalDataTemplate

  <HierarchicalDataTemplate x:Key="Folders" ItemsSource="{Binding Folders}" >
        <StackPanel Orientation="Horizontal">                
            <TextBlock Foreground="Black" Text="{Binding Name}" />
        </StackPanel>
  </HierarchicalDataTemplate>

从循环列表中查找父类

属性将冒泡并暴露,因为这是c#继承的工作方式。只要直接绑定到基本属性,就像它们在文件夹上一样。

public class BindableBase
{
    public string InheritedName { get; set; }
}
public class Folder : BindableBase
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value;  }
    }
    private List<Folder> subfolders=new List<Folder>();
    public List<Folder> Folders
    {
        get { return subfolders; }
        set{ subfolders= value; }
    }
}
XAML:

<TextBlock Foreground="Black" Text="{Binding InheritedName}" />