1

我遇到的情况是 ContextMenu 的项目数已达到 3000 左右。在这种情况下,ContextMenu 会花费大量时间来加载自身。我正在尝试进行 UI 虚拟化,但没有任何运气。

示例代码如下:

 <RichTextBox x:Name="FMWpfPopupControlHost" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True"  HorizontalAlignment="Left" Width="300" Height="600" Focusable="True" Loaded="onLoaded1">
        <RichTextBox.ContextMenu >
            <ContextMenu VirtualizingStackPanel.IsVirtualizing="True" ItemsSource="{Binding GetLabel, Source ={StaticResource model}}" ScrollViewer.CanContentScroll="True">
                <ContextMenu.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Initialized="TextBlock_Initialized" Text="{Binding getLabel}"/>
                    </DataTemplate>
                </ContextMenu.ItemTemplate>
                <ContextMenu.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel IsItemsHost="True" IsVirtualizing="True" VirtualizationMode="Recycling"/>                        
                    </ItemsPanelTemplate>
                </ContextMenu.ItemsPanel>
                <ContextMenu.Template>
                    <ControlTemplate>
                        <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
                            <ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False">
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </ContextMenu.Template>
            </ContextMenu>
        </RichTextBox.ContextMenu>
    </RichTextBox>

请指导我如何解决这个问题?

谢谢!

苛刻的古普塔

4

0 回答 0