Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到树视图_C#_Wpf_Xaml_Binding_Treeview - Fatal编程技术网

C# 绑定到树视图

C# 绑定到树视图,c#,wpf,xaml,binding,treeview,C#,Wpf,Xaml,Binding,Treeview,我正在尝试将一个带有字典的类绑定到树视图。要绑定的对象的基类是ElementT: // Base.cs in Base.dll public class ElementT { protected ElementT mParent; private string mKey; protected Dictionary<string, ElementT> mMembers; //... public ElementT(string nodeKey) { mKey =

我正在尝试将一个带有字典的类绑定到树视图。要绑定的对象的基类是ElementT:

// Base.cs in Base.dll

public class ElementT {

  protected ElementT mParent;
  private string mKey;
  protected Dictionary<string, ElementT> mMembers;
  //...
  public ElementT(string nodeKey) { mKey = nodeKey; }
  //...
  public ElementT Parent {
    get {return mParent}
    protected set { mParent = value; }
    }
  public string Key => mKey;
  public Dictionary<string, ElementT> Members => mMembers;
  //...
  }

public class NamespaceT : ElementT {

  public NamespaceT(string key) : base(key) {}
}
在引用Engine.dll的wpf窗口类中:

// MainWindow.xaml.cs (references Engine.dll) 

public partial class MainWindow : Window {

private EngineT mEngine;
private ObservableCollection<ElementT> mElementList;

public MainWindow() {

  mEngine = new EngineT();
  mElementList = new ObservableCollection<ElementT>();
  mElementList.Add(mEngine.RootElement);

  InitializeComponent();


  //ElementLibraryTreeView.ItemsSource = elementList; ????

  }
}
//MainWindow.xaml.cs(引用Engine.dll)
公共部分类主窗口:窗口{
私人发动机;
私人可观察收集名单;
公共主窗口(){
mEngine=新引擎();
mElementList=新的ObservableCollection();
mElementList.Add(mEngine.RootElement);
初始化组件();
//ElementLibraryTreeView.ItemsSource=elementList????
}
}
在XAML中:

   <TreeView ItemsSource="{Binding Source={StaticResource local:mElementList}}">
      <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type Key}" ItemsSource="{Binding Path=Members}">
          <TextBlock Text="{Binding Path=Key}"/>
        </HierarchicalDataTemplate>
      </TreeView.Resources>


    </TreeView>


我的目标是将键(mKey)和字典成员(mMembers)的成员列为n个级别的子级。此外,我需要能够根据派生类类型(如名称空间)更改树显示。从ElementT派生出许多不同的类型。我眼前的问题是将ElementT的根实例绑定到代码中的treeview。如果能得到任何帮助,为我指明正确的方向,我将不胜感激。谢谢。

你的“=>”代表什么?这是一个表达式体属性,是C#6.0中的一个新特性。这是一个仅getter属性的新语法。你的“=>”代表什么?这是一个表达式体属性,是C#6.0中的一个新特性。这是一个仅getter属性的新语法。
   <TreeView ItemsSource="{Binding Source={StaticResource local:mElementList}}">
      <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type Key}" ItemsSource="{Binding Path=Members}">
          <TextBlock Text="{Binding Path=Key}"/>
        </HierarchicalDataTemplate>
      </TreeView.Resources>


    </TreeView>