首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF树视图子节点消失

WPF树视图子节点消失
EN

Stack Overflow用户
提问于 2022-05-03 05:54:08
回答 1查看 34关注 0票数 -1

我是wpf和xaml的新手。我刚刚在具有相同类型的单独选项卡上构建了4个复杂的树视图,其中的数据是从一个固定的json文件中加载的。但我发现了一些奇怪的东西:

当第一次加载

  1. 时,一些节点不显示小三角形,这意味着它不包含任何子项,但实际上它们包含。(我在父节点上有一个数字,上面写着“10”),
  2. 只发生在树的不可见区域的节点上。这意味着如果我滚动到节点,然后重新加载树,一切正常。但是,如果我滚动到一个在加载树时不可查看的节点,很有可能它不会显示三角形和子项。当我在这些选项卡之间切换时,有时树会像上面所描述的那样发生变化,即使没有滚动happens.
  3. Initialing,使用IsExpand = True / False的节点也没有区别。

以下是xaml:

代码语言:javascript
运行
复制
<resultTrees:ResultTreeView x:Class="ChiSharedFormsWpf.ResultTrees.ChiTreeView"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                        xmlns:fa5="http://schemas.fontawesome.com/icons/"
                        xmlns:bsiIndexes="clr-namespace:ChiLibApiJson.BsiIndexes;assembly=ChiLibApiJson"
                        xmlns:dirTrees="clr-namespace:WpfUtilities.DirTrees;assembly=WpfUtilities"
                        xmlns:resultTrees="clr-namespace:ChiSharedFormsWpf.ResultTrees"
                        mc:Ignorable="d" FontFamily="Consolas"
                        MouseDoubleClick="ChiTreeView_OnMouseDoubleClick"
                        d:DesignHeight="450" Width="auto">
<Grid>
    <DockPanel HorizontalAlignment="Stretch">
        <TreeView Name="Tree" HorizontalAlignment="Stretch"
                  VirtualizingStackPanel.IsVirtualizing="True"
                  VirtualizingStackPanel.VirtualizationMode="Recycling"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <TreeView.Resources>
                <!-- Style the inactive selection the same as active -->
                <SolidColorBrush Color="LightBlue" x:Key="{x:Static SystemColors.HighlightBrushKey}" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                                 Color="LightBlue" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
                                 Color="OrangeRed" />

                <HierarchicalDataTemplate DataType="{x:Type dirTrees:BriefTree}"
                                          ItemsSource="{Binding Items}">
                    <StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <fa5:FontAwesome Icon="{Binding Icon}" Margin="0,2,5,0" />
                            <TextBlock Text="{Binding Root.Text}" FontFamily="微软雅黑, Consolas" />
                        </StackPanel>
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type resultTrees:FileResultList}"
                                          ItemsSource="{Binding FileResultBriefs}">
                    <StackPanel Orientation="Horizontal"
                                MouseRightButtonUp="TreeNode_OnMouseRightButtonUp">
                        <fa5:FontAwesome Icon="{Binding Icon}" Margin="0,2,5,0" />
                        <TextBlock FontWeight="Bold" Text="{Binding Root.Text}" />
                        <TextBlock FontWeight="Bold" Text="{Binding FileResultBriefs.Count}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type dirTrees:DirAndFileResultTree}"
                                          ItemsSource="{Binding SubDirsAndFilesAndBadSmells}">
                    <StackPanel Orientation="Horizontal" Background="{Binding DirColorHex}"
                                MouseRightButtonUp="TreeNode_OnMouseRightButtonUp"
                                Tag="{Binding Root.DirName }">
                        <fa5:FontAwesome Icon="Regular_FolderOpen" Margin="0,2,5,0" />
                        <TextBlock FontWeight="Bold" Text="{Binding Index.Brief}" />
                        <TextBlock FontWeight="Bold" Text="{Binding Root.DirName}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type dirTrees:FileResultBrief}"
                                          ItemsSource="{Binding BadSmells}">
                    <StackPanel Orientation="Horizontal" Background="{Binding  Index.SoftColorHex}"
                                MouseLeftButtonDown="FileResult_OnMouseLeftButtonDown"
                                MouseRightButtonUp="TreeNode_OnMouseRightButtonUp"
                                Tag="{Binding  FileName}">
                        <fa5:FontAwesome Icon="Regular_FileCode" Margin="0,2,5,0" />
                        <TextBlock Text="{Binding Brief}" />
                        <TextBlock Text="{Binding FileName}" Margin="10 0"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type bsiIndexes:BadSmell}" ItemsSource="{Binding }">
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="TreeViewItem">
                            <Style.Triggers>
                                <DataTrigger
                                    Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsWarning }"
                                    Value="True">
                                    <Setter Property="Visibility" Value="Hidden" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <StackPanel Orientation="Horizontal"
                                MouseLeftButtonDown="BadSmell_OnMouseLeftButtonDown"
                                Tag="{Binding}">
                        <fa5:FontAwesome Icon="{Binding XamlIcon}"
                                         Foreground="{Binding Path=ForeColorHex}" Margin="0,2,5,0" />
                        <StackPanel>
                            <TextBlock Text="{Binding Path=LevelAndRuleNameAndExplanation}"
                                       FontFamily="微软雅黑, Consolas"
                                       Foreground="{Binding Path=ForeColorHex}" />
                            <TextBlock Text="{Binding Path=PositionAndDescription}"
                                       FontFamily="微软雅黑, Consolas"
                                       Foreground="{Binding Path=ForeColorHex}" />
                        </StackPanel>
                    </StackPanel>
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type dirTrees:BriefString}">
                    <TextBlock Text="{Binding Path=Text}" Foreground="{Binding Path=ForeColorHex}"
                               FontFamily="微软雅黑, Consolas" />
                </DataTemplate>
                <DataTemplate DataType="{x:Type dirTrees:IgnoredDirString}">
                    <StackPanel MouseRightButtonUp="TreeNode_OnMouseRightButtonUp"
                                Tag="{Binding Text}" Orientation="Horizontal">
                        <fa5:FontAwesome Icon="Solid_Folder" Foreground="gray"></fa5:FontAwesome>
                        <TextBlock Text="{Binding Path=Text}"  Foreground="gray"
                                   TextDecorations="Strikethrough" 
                                   FontFamily="微软雅黑, Consolas" />
                    </StackPanel>
                </DataTemplate>
                <DataTemplate DataType="{x:Type dirTrees:IgnoredFileString}">
                    <StackPanel MouseRightButtonUp="TreeNode_OnMouseRightButtonUp"
                                Tag="{Binding Text}" Orientation="Horizontal">
                        <fa5:FontAwesome Icon="Regular_File" Foreground="gray"></fa5:FontAwesome>
                        <TextBlock Text="{Binding Path=Text}"  Foreground="gray"
                                   TextDecorations="Strikethrough" 
                                   FontFamily="微软雅黑, Consolas" />
                    </StackPanel>
                </DataTemplate>
            </TreeView.Resources>
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <!-- <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=OneTime}" /> -->
                    <!-- <Setter Property="IsExpanded" Value="True" /> -->
                    <Setter Property="IsExpanded" Value="False" />
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
    </DockPanel>
</Grid>
</resultTrees:ResultTreeView>

以下是模型的部分内容:

代码语言:javascript
运行
复制
public List<FileResultBrief> FileResultBriefs { get; set; } = new();
public List<DirAndFileResultTree> SubTrees { get; set; } = new();

[JsonIgnore]
public IList SubDirsAndFilesAndBadSmells => new CompositeCollection
{
    new CollectionContainer {Collection = IgnoredFiles.OrderBy(i => i.Text).ToList()},
    new CollectionContainer {Collection = FileResultBriefs},
    new CollectionContainer {Collection = IgnoredDirs.OrderBy(i => i.Text).ToList()},
    new CollectionContainer {Collection = SubTrees},
};

到目前为止,我非常确信模型中可能存在一些共享引用错误。但是我有人能找出xaml中的问题会很有帮助。因为我对它的机制知之甚少。

非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2022-05-03 15:01:16

最后,删除

代码语言:javascript
运行
复制
VirtualizingStackPanel.VirtualizationMode="Recycling"

或使用

代码语言:javascript
运行
复制
VirtualizingStackPanel.VirtualizationMode="Standard"

反而解决了问题。我在两周前增加了这一行,以提高性能。根据https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.virtualizationmode?view=windowsdesktop-6.0的说法,如果treeview包含不同类型的容器(我认为这几乎总是正确的),则不应该使用回收模式。

所以这是专为列表设计的吗?

我采纳了O_W的建议,将列表更改为ObservableList。在这种情况下没有什么区别,但以后可能会有所帮助,谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72095287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档