我是wpf和xaml的新手。我刚刚在具有相同类型的单独选项卡上构建了4个复杂的树视图,其中的数据是从一个固定的json文件中加载的。但我发现了一些奇怪的东西:
当第一次加载
以下是xaml:
<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>
以下是模型的部分内容:
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中的问题会很有帮助。因为我对它的机制知之甚少。
非常感谢。
发布于 2022-05-03 15:01:16
最后,删除
VirtualizingStackPanel.VirtualizationMode="Recycling"
或使用
VirtualizingStackPanel.VirtualizationMode="Standard"
反而解决了问题。我在两周前增加了这一行,以提高性能。根据https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.virtualizationmode?view=windowsdesktop-6.0的说法,如果treeview包含不同类型的容器(我认为这几乎总是正确的),则不应该使用回收模式。
所以这是专为列表设计的吗?
我采纳了O_W的建议,将列表更改为ObservableList。在这种情况下没有什么区别,但以后可能会有所帮助,谢谢。
https://stackoverflow.com/questions/72095287
复制相似问题