将复合对象绑定到树视图 WPF
Binding Composite Object to Tree View WPF
我有一个遵循复合设计模式的对象。我想使用树视图在 WPF 中显示此对象,但我无法正确绑定数据。我有两个 classes:Leaf,简单 class,没有任何子元素,Box,复合 class,有子元素,可以是 Leaf class 或 Box class。我还有一个名为 ITree
的通用接口
界面
public interface ITree
{
string Name { get; }
string Property1 { get; }
string Property2 { get; }
}
简单class
public class Leaf : ITree
{
string ITree.Name { get { return _name; } }
string ITree.Property1 { get { return property1; } }
string ITree.Property2 { get { return property2; } }
}
复合class
public class Box : ITree
{
string ITree.Name { get { return _name; } }
string ITree.Property1 { get { return property1; } }
string ITree.Property2 { get { return property2; } }
List<ITree> Children = new List<ITree>();
}
xaml.cs
List<ITree> ListToBind = new List<ITree>();
ITree finalObject = PopulateCompositeObjeectWithData();
ListToBind.Add(finalObject);
xaml
<TreeView ItemsSource="{Binding ElementName=Window, Path= ListToBind}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我想要实现的树视图:
Box - Name
|-Leaf - Name
|-Leaf - Name
|-Box - Name
| |-Leaf - Name
| |-Leaf - Name
任何建议或代码示例将不胜感激
谢谢
首先,Children
必须是 public 属性 才能绑定到它:
public class Box : ITree
{
string ITree.Name { get { return _name; } }
string ITree.Property1 { get { return property1; } }
string ITree.Property2 { get { return property2; } }
public List<ITree> Children { get; } = new List<ITree>();
}
其次,您应该像这样使用括号绑定到显式实现的接口成员:
<TreeView ItemsSource="{Binding ElementName=Window, Path= ListToBind}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding (local:ITree.Name)}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我有一个遵循复合设计模式的对象。我想使用树视图在 WPF 中显示此对象,但我无法正确绑定数据。我有两个 classes:Leaf,简单 class,没有任何子元素,Box,复合 class,有子元素,可以是 Leaf class 或 Box class。我还有一个名为 ITree
的通用接口界面
public interface ITree
{
string Name { get; }
string Property1 { get; }
string Property2 { get; }
}
简单class
public class Leaf : ITree
{
string ITree.Name { get { return _name; } }
string ITree.Property1 { get { return property1; } }
string ITree.Property2 { get { return property2; } }
}
复合class
public class Box : ITree
{
string ITree.Name { get { return _name; } }
string ITree.Property1 { get { return property1; } }
string ITree.Property2 { get { return property2; } }
List<ITree> Children = new List<ITree>();
}
xaml.cs
List<ITree> ListToBind = new List<ITree>();
ITree finalObject = PopulateCompositeObjeectWithData();
ListToBind.Add(finalObject);
xaml
<TreeView ItemsSource="{Binding ElementName=Window, Path= ListToBind}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我想要实现的树视图:
Box - Name
|-Leaf - Name
|-Leaf - Name
|-Box - Name
| |-Leaf - Name
| |-Leaf - Name
任何建议或代码示例将不胜感激
谢谢
首先,Children
必须是 public 属性 才能绑定到它:
public class Box : ITree
{
string ITree.Name { get { return _name; } }
string ITree.Property1 { get { return property1; } }
string ITree.Property2 { get { return property2; } }
public List<ITree> Children { get; } = new List<ITree>();
}
其次,您应该像这样使用括号绑定到显式实现的接口成员:
<TreeView ItemsSource="{Binding ElementName=Window, Path= ListToBind}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding (local:ITree.Name)}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>