为什么 ComboBox 下拉导致整个应用程序的性能很慢? [英] Why ComboBox be dropdown result whole app performance to very slow?

查看:49
本文介绍了为什么 ComboBox 下拉导致整个应用程序的性能很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wpf 应用程序.有一件奇怪的事情,只要我按下鼠标,ComboBox 下拉箭头就会变得非常慢,无论 ComboBox 是否有项目.烦人的情况只出现在某些计算机上.期待回复.

I have a wpf application.There is a strange thing that as long as i mousedown the ComboBox droparrow becomes all UI response very slow, regardless of whether ComboBox have items. Annoying the case only appears on some computers. Look forward to answering.

推荐答案

我已经查看了您可以在 Internet 上找到的提示,但我发现没有任何问题.

I have looked out the tips that you can find on the internet and I just noticed there are no problems.

以下几点要慎重,否则虚拟化就没了(摘自MSDN,链接):

The following things should be followed carefully, because otherwise the virtualization is gone (taken from MSDN, link):

  • 项目容器直接添加到 ItemsControl.例如,如果应用程序将 ListBoxItem 对象显式添加到 ListBox,则 ListBox 不会虚拟化 ListBoxItem 对象.
  • 将 CanContentScroll 设置为 false.
  • 将 IsVirtualizing 设置为 false.
  • 使用项目分组.

以下样式对我来说没有问题.

The following style works for me without problems.

<Style x:Key="SimpleComboBox" TargetType="{x:Type ComboBox}">
  <Setter Property="ItemsPanel">
    <Setter.Value>
      <ItemsPanelTemplate>
        <VirtualizingStackPanel IsItemsHost="True" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" KeyboardNavigation.DirectionalNavigation="Contained" />
      </ItemsPanelTemplate>
    </Setter.Value>
  </Setter>
</Style>

<ComboBox Name="cbTest" IsEditable="True" Style="{StaticResource SimpleComboBox}" />


在我以 Kaxaml 的风格玩了一段时间后,我注意到了一件小事.使用这种样式时,我在使用 IsEditable = "True" 时遇到了问题,性能下降了!


After I played around for a while with the style of Kaxaml, I noticed a small thing. With this style I've had problems when I used IsEditable = "True", the performance goes down!

以下风格对我来说很完美

The following style works for me perfect

<Style x:Key="SimpleComboBox" TargetType="{x:Type ComboBox}">
  <Setter Property="SnapsToDevicePixels" Value="true"/>
  <Setter Property="OverridesDefaultStyle" Value="true"/>
  <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
  <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
  <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
  <Setter Property="MinWidth" Value="120"/>
  <Setter Property="MinHeight" Value="20"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ComboBox}">
        <Grid>
          <ToggleButton Name="ToggleButton" Focusable="false" ClickMode="Press"
                        IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}">
            <ToggleButton.Template>
              <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Grid>
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="20" />
                  </Grid.ColumnDefinitions>
                  <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="2" Background="#C0C0C0" BorderBrush="#404040" BorderThickness="1" />
                  <Border Grid.Column="0" CornerRadius="2,0,0,2" Margin="1" Background="#FFFFFF" BorderBrush="#404040" BorderThickness="0,0,1,0" />
                  <Path x:Name="Arrow" Grid.Column="1" Fill="#404040" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
                </Grid>
                <ControlTemplate.Triggers>
                  <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#808080" />
                  </Trigger>
                  <Trigger Property="ToggleButton.IsChecked" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#E0E0E0" />
                  </Trigger>
                  <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                    <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                    <Setter Property="Foreground" Value="#888888"/>
                    <Setter TargetName="Arrow" Property="Fill" Value="#888888" />
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </ToggleButton.Template>
          </ToggleButton>
          <ContentPresenter Name="ContentSite" IsHitTestVisible="False"
            Content="{TemplateBinding SelectionBoxItem}"
            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
            Margin="3,3,23,3"
            VerticalAlignment="Center"
            HorizontalAlignment="Left" />
          <TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" HorizontalAlignment="Left" VerticalAlignment="Center"
                   Margin="3,3,23,3" Focusable="True" Background="Transparent" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}">
            <TextBox.Template>
              <ControlTemplate TargetType="{x:Type TextBox}">
                <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
              </ControlTemplate>
            </TextBox.Template>
          </TextBox>
          <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
            <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
              <Border x:Name="DropDownBorder" Background="#FFFFFF" BorderThickness="1" BorderBrush="#888888"/>
              <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                <VirtualizingStackPanel IsItemsHost="True" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" KeyboardNavigation.DirectionalNavigation="Contained" />
              </ScrollViewer>
            </Grid>
          </Popup>
        </Grid>
        <ControlTemplate.Triggers>
          <Trigger Property="HasItems" Value="false">
            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
          </Trigger>
          <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="#888888"/>
          </Trigger>
          <!-- remove this trigger, because the virtualization is broken!!!
          <Trigger Property="IsGrouping" Value="true">
            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
          </Trigger>
          -->                
          <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
            <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
            <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
          </Trigger>
          <Trigger Property="IsEditable"
                   Value="true">
            <Setter Property="IsTabStop" Value="false"/>
            <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Style.Triggers>
  </Style.Triggers>
</Style>

我也删除了这个触发器.

I have also removed this trigger.

<Trigger Property="IsGrouping" Value="true">
  <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>

希望能帮到你

这篇关于为什么 ComboBox 下拉导致整个应用程序的性能很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆