wpf - 如何使组合框更快地添加项目?
问题描述
我用 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 等都不会喜欢这样。
我怎样才能让它更快地添加?请帮帮我,谢谢。
解决方案
您可以尝试使用 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>
推荐阅读
- c# - 使用元素位置使用循环打印存储在该位置的值
- sql - org.h2.jdbc.JdbcSQLException:未知数据类型:“DATEADD”;SQL 语句
- c# - Internet Explorer 11 Xunit 上的点击问题
- python - youtube 缩略图没有出现在 Jupyter 笔记本中
- c++ - 在 C++ 中访问指针类型 char 数组
- airconsole - 我可以在空调上开发一个主屏幕上的按比例缩小版本的控制器吗?
- visual-studio-code - 使用扩展 API 将 EOL 更改为“LF”
- javascript - 如何在 Angular 应用程序中捕获“net::ERR_CERT_AUTHORITY_INVALID”以处理 SSL 证书问题?
- python - Python - 将数据从列表添加到表时出错
- powershell - 我可以隐藏 Import-Module 的定义吗?