当使用鼠标滚轮滚动时,WPF树视图出现了奇怪的问题

本文关键字:视图 问题 WPF 鼠标 滚动 | 更新日期: 2023-09-27 18:08:03

所以,我一直在为我们的地图绘制图例,使用WPF树视图来显示分组和图层。

我已经得到它的工作和显示很好,但是当我用鼠标滚轮滚动树视图时,控件开始闪烁,树的垂直滚动条不断上下调整大小。

树视图的布局是这样的:

  • 集团
      • 图层子项
      • 图层子项
      • 图层子项
  • 集团
    • 等等…

Group和Layer节点是树视图项,但层子项包含在项控件中。层子项不能被展开/收缩或选择,因此必须在层节点下保持静态,因此项控件似乎是一个明智的选择。

当我用鼠标滚轮滚动到树形视图的顶部或底部时,滚动条开始晃动和调整大小,项目控件的最后几个元素在视图内和视图外闪烁(当它根本不应该在视图内时),有时,树形视图实际上会来回滚动。

如果我删除项目控制,一切工作,因为它应该。当我把它加回去的时候,它就乱了。

同样,如果我用鼠标抓住滚轮的拇指并拖动它,一切都很好。不要乱跳。

下面是控件的资源XAML:

        <views:DynamicLegendNodeTemplateSelector x:Key="LegendTemplateSelector">
        <views:DynamicLegendNodeTemplateSelector.GroupTemplate>
            <HierarchicalDataTemplate DataType="{x:Type legend:IDynamicMapLegendGroup}">
                <HierarchicalDataTemplate.ItemsSource>
                    <MultiBinding Converter="{StaticResource LegendNode}">
                        <Binding Path="Groups"/>
                        <Binding Path="LegendLayers"/>
                    </MultiBinding>
                </HierarchicalDataTemplate.ItemsSource>
                <Grid>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Focusable="False" IsChecked="{Binding IsVisible}" VerticalAlignment="Center">
                            <TextBlock Text="{Binding DisplayName}" VerticalAlignment="Center"/>
                        </CheckBox>
                    </StackPanel>
                </Grid>
            </HierarchicalDataTemplate>
        </views:DynamicLegendNodeTemplateSelector.GroupTemplate>
        <views:DynamicLegendNodeTemplateSelector.LayerTemplate>
            <HierarchicalDataTemplate DataType="{x:Type legend:IDynamicMapLayerLegendItem}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <CheckBox Grid.Row="0" Focusable="False" IsChecked="{Binding IsVisible}" VerticalAlignment="Center">
                        <TextBlock Text="{Binding LayerCaption}" VerticalAlignment="Center"/>
                    </CheckBox>
                    <ItemsControl Grid.Row="1"
                                 Margin="16,0,0,0" 
                                BorderThickness="0"
                                Background="Transparent"
                                ItemsSource="{Binding LegendItems, IsAsync=True}"
                                HorizontalAlignment="Left"
                                HorizontalContentAlignment="Left"
                                  MouseWheel="ItemControls_MouseWheel"
                                  ScrollViewer.CanContentScroll="False"
                                MouseUp="ItemsControl_MouseUp">
                            <ItemsControl.Template>
                                <ControlTemplate>
                                    <ItemsPresenter/>
                                </ControlTemplate>
                            </ItemsControl.Template>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="30"/>
                                                <ColumnDefinition Width="Auto"/>
                                            </Grid.ColumnDefinitions>
                                            <Image Grid.Column="0" Width="20" Height="20" Stretch="UniformToFill" Source="{Binding Symbol}"/>
                                            <Label Grid.Column="1" Content="{Binding Label}"/>
                                        </Grid>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                </Grid>
            </HierarchicalDataTemplate>
        </views:DynamicLegendNodeTemplateSelector.LayerTemplate>
    </views:DynamicLegendNodeTemplateSelector>
    <Style x:Key="TreeItemStyle" TargetType="TreeViewItem">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <EventSetter Event="MouseUp" Handler="TreeViewItem_MouseUp"></EventSetter>
    </Style>

这是treeview:

<TreeView x:Name="LegendHierarchy" 
              MinWidth="200"
              ItemsSource="{Binding LegendItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DynamicArcGisRuntimeMapLegendView}}}"
              ItemContainerStyle="{StaticResource TreeItemStyle}"
              ItemTemplateSelector="{StaticResource LegendTemplateSelector}" />

如果需要的话,这段代码是在Visual Studio 2015中使用。net 4.5编写的。

不管怎样,有人知道是什么导致了这个问题吗?

谢谢

当使用鼠标滚轮滚动时,WPF树视图出现了奇怪的问题

所以,这表明晚上睡个好觉是有帮助的。

显然,我所要做的就是设置
VirtualizingPanel.VirtualizationMode="Recycling"

在treeview控件上,它开始工作。

下面是完整的树视图XAML:
<TreeView x:Name="LegendHierarchy" 
              MinWidth="200"
              ItemsSource="{Binding LegendItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DynamicArcGisRuntimeMapLegendView}}}"
              ItemContainerStyle="{StaticResource TreeItemStyle}"
              ItemTemplateSelector="{StaticResource LegendTemplateSelector}" UseLayoutRounding="True" ScrollViewer.CanContentScroll="True" HorizontalContentAlignment="Stretch" 
              VirtualizingPanel.VirtualizationMode="Recycling"/>