【问题标题】:Weird issue with WPF TreeView when using mouse wheel to scroll使用鼠标滚轮滚动时 WPF TreeView 出现奇怪问题
【发布时间】:2016-09-20 22:49:12
【问题描述】:

所以,我一直在为我们的地图制作图例,使用 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 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。

无论如何,有谁知道可能导致问题的原因?

谢谢

【问题讨论】:

    标签: c# wpf treeview itemscontrol mousewheel


    【解决方案1】:

    因此,这表明尝试睡个好觉是有帮助的。

    显然,我所要做的就是设置

    VirtualizingPanel.VirtualizationMode="Recycling"
    

    在树视图控件上,它开始工作了。

    这是 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"/>
    

    我希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多