首页 > 解决方案 > 如何使组合框更快地添加项目?

问题描述

我用 aComboBox来加载电脑上的所有字体并进行预览。

这是 XAML:

<Window x:Class="Sample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Sample"
        mc:Ignorable="d"
        Title="Demo" Height="450" Width="800" WindowStartupLocation="CenterScreen" WindowStyle="None" Loaded="Window_Loaded" Background="White">
    <Window.Resources>
        <local:FontFamilyConverter x:Key="FontFamilyConverter"></local:FontFamilyConverter>
    </Window.Resources>
    <Grid>
        <ComboBox  Margin="10,10,0,10" HorizontalContentAlignment="Stretch" Name="FontFaimlyCB" Height="50" Width="250" ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" FontFamily="{Binding Converter={StaticResource FontFamilyConverter}}" FontSize="20"></TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

这是代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Globalization;

namespace Sample
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {            
        }
    }
    public class FontFamilyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return new FontFamily(value.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

当我第一次点击ComboBox时,添加项目总是需要很长时间(我的电脑上有将近一千种字体,需要3-4秒才能完成)。但任何其他软件,如 word/photoshop 等都不会喜欢这样。

我怎样才能让它更快地添加?请帮帮我,谢谢。

标签: wpf

解决方案


您可以尝试使用 aVirtualizingStackPanel作为 theItemsPanel并将 the 设置MaxDropDownHeight为一个相当小的值,以免立即呈现所有容器。而且您不必使用转换器:

<ComboBox  Margin="10,10,0,10" HorizontalContentAlignment="Stretch" Name="FontFaimlyCB" Height="50" Width="250" 
           ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"
            MaxDropDownHeight="50">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" FontFamily="{Binding}" FontSize="20" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

推荐阅读