我有两个班级
public class leaf
{
public string name { get; set; }
}
和
public class node
{
public ObservableCollection<node> nodeList = new ObservableCollection<node>();
public ObservableCollection<leaf> leafList = new ObservableCollection<leaf>();
public ObservableCollection<node> prop_nodeList { get { return nodeList; } set { nodeList = value; } }
public ObservableCollection<leaf> prop_leafList { get { return leafList; } set { leafList = value; } }
public string name { get; set; }
}
因为你可以看到它的工作像道路或树木。节点可以有另一个节点和叶子的信息。
我希望在用户控件树中显示,就像那样
nethead是一个主节点,它有另外两个节点(b,a)。节点a有两个节点(b,c)和2个叶子(a1,a2)。但我不用mvvm这样做。当我用mvvm做它看起来像
来自C#im只做
this.DataContext = mainNode; // its node which hold everething (its named netHead)
来自xaml的
<Grid>
<TreeView ItemsSource="{Binding prop_nodeList}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:node}" ItemsSource="{Binding prop_nodeList}">
<TextBlock Text="{Binding Path=name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:leaf}" ItemsSource="{Binding prop_leafList}">
<TextBlock Text="{Binding Path=name}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
你知道我想做什么吗?我想在TreeView中制作双treeItem,但它不起作用:(
请帮助,它的Ant算法的项目,我想在教室里做最好的gui
答案 0 :(得分:1)
对您的代码进行了一些修改:实现了根视图模型NodesData,只是为了演示和创建启动数据。还添加了属性Children为节点的子节点提供单个属性。并且还略微修改了xaml以使用单层次数据模板。似乎它有效。
public class leaf
{
public string name { get; set; }
}
public class node
{
public string name { get; set; }
public ObservableCollection<node> nodeList = new ObservableCollection<node>();
public ObservableCollection<leaf> leafList = new ObservableCollection<leaf>();
public ObservableCollection<node> prop_nodeList { get { return nodeList; } set { nodeList = value; } }
public ObservableCollection<leaf> prop_leafList { get { return leafList; } set { leafList = value; } }
public ObservableCollection<object> Children
{
get
{
var children = prop_nodeList.OfType<object>();
return new ObservableCollection<object>(children.Concat(prop_leafList.OfType<object>()));
}
}
}
public class NodesData
{
public ObservableCollection<node> RootSource { get; set; }
public NodesData()
{
var rootNode = new node() { name = ">>>Head Node<<<<" };
var b = new node() { name = "b" };
var c = new node() { name = "c" };
var a = new node()
{
name = "a",
prop_nodeList = new ObservableCollection<node>() { b, c },
prop_leafList = new ObservableCollection<leaf>() { new leaf() { name = "a1" }, new leaf() { name = "a2" } }
};
rootNode.prop_nodeList = new ObservableCollection<node>() { b, a };
RootSource = new ObservableCollection<node>() { rootNode };
}
}
}
XAML:
<Grid>
<Grid.Resources>
<wpfApplication1:NodesData x:Key="dataSource"/>
</Grid.Resources>
<TreeView ItemsSource="{Binding RootSource}" DataContext="{StaticResource dataSource}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type wpfApplication1:node}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Path=name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type wpfApplication1:leaf}" >
<TextBlock Text="{Binding Path=name}"/>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
答案 1 :(得分:0)
首先,如果您希望显示根节点,则需要将ItemsSource设置为包含根节点的集合。
至于没有显示的叶子......你需要一个DataTemplate,而不是HierarchicalDataTemplate。我怀疑绑定错误会阻止它正确呈现,因为leaf没有子集合。
刚才注意到这是一个家庭作业问题,所以我不会再坚持下去了。查看绑定错误(启用WPF跟踪以在输出窗口中查看它们)并尝试从中找出它。