gpt4 book ai didi

c# - 递归 TreeView

转载 作者:行者123 更新时间:2023-11-30 23:05:05 25 4
gpt4 key购买 nike

我正在尝试在打开以下模型的 IsVirtualizing 的情况下执行递归 TreeView:

    public class DataContainer
{
public List<DataEntity> Entities { get; set; }
}

public class DataEntity
{
public uint m_dwID { get; set; }

public int m_nMargineH { get; set; }
public int m_nMargineV { get; set; }
public int m_nPosX { get; set; }
public int m_nPosY { get; set; }

public List<DataEntity> m_pNEXT { get; set; }
}

我已经尝试使用 Microsoft 的示例进行工具化,但我无法使其正常工作 (https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-improve-the-performance-of-a-treeview#code)
它要么显示正确的第一级项目名称而没有展开按钮(以显示其子项目),要么显示类似“(集合)”的内容

我不是 WPF 专家所以请多多包涵 :)

最佳答案

您必须以递归设置 m_pNEXT 子级的方式设置 HierarchicalDataTemplate,这是一个示例:

WPF:

<TreeView Name="MyTreeView" HorizontalAlignment="Left" Height="177" Margin="126,76,0,0" VerticalAlignment="Top" Width="247">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:DataEntity}" ItemsSource="{Binding m_pNEXT}">
<TextBlock Text="{Binding m_dwID}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>

DataContainer 示例:

MyTreeView.ItemsSource = new DataContainer
{
Entities = new List<DataEntity>
{
new DataEntity
{
m_dwID = 1,
m_pNEXT = new List<DataEntity>
{
new DataEntity
{
m_dwID = 11
},
new DataEntity
{
m_dwID = 12
}
}
},

new DataEntity
{
m_dwID = 2,
m_pNEXT = new List<DataEntity>
{
new DataEntity
{
m_dwID = 21,
m_pNEXT = new List<DataEntity>
{
new DataEntity
{
m_dwID = 211
}
}
}
}
}
}
}.Entities;

结果:

Result

您现在所要做的就是使用 HierarchicalDataTemplate 并根据需要显示每个项目,希望这对您有所帮助!

关于c# - 递归 TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48963599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com