WPF 自定义分列下拉框,支持模糊查询和大量数据

本文将介绍如何创建一个自定义的下拉框类,该下拉框可以分列显示数据,并且支持根据输入的字符进行模糊查询。此外,它还能够承受几万行数据,并附带了代码示例和详细注释。

1. 创建自定义下拉框类

首先,我们需要创建一个自定义的下拉框类,继承自 ComboBox 类:

public class MyComboBox : ComboBox
{
    // ...
}

2. 添加属性和方法

在类中,我们需要添加一些必要的属性和方法:

public class MyComboBox : ComboBox
{
    // 数据源
    private IEnumerable _itemsSource;

    // 每列的宽度
    public List<double> ColumnWidths { get; set; } 

    // 显示列的属性名称
    public List<string> DisplayMemberPaths { get; set; } 

    // 模糊查询的属性名称
    public string FilterMemberPath { get; set; } 

    // 构造函数
    public MyComboBox()
    {
        // 初始化属性
        ColumnWidths = new List<double>();
        DisplayMemberPaths = new List<string>();
        FilterMemberPath = '';
    }

    // 设置数据源
    public void SetItemsSource(IEnumerable itemsSource)
    {
        _itemsSource = itemsSource;
        ItemsSource = _itemsSource;
    }

    // 根据输入的字符模糊查询并显示数据
    public void Filter(string text)
    {
        if (_itemsSource == null) return;

        // 根据输入的字符进行模糊查询
        var filteredItems = _itemsSource.Cast<object>()
            .Where(item => item.GetType().GetProperty(FilterMemberPath).GetValue(item).ToString().Contains(text))
            .ToList();

        // 清空原有数据并添加新数据
        Items.Clear();
        foreach (var item in filteredItems)
        {
            var content = new StackPanel();
            for (int i = 0; i < DisplayMemberPaths.Count; i++)
            {
                var propertyValue = item.GetType().GetProperty(DisplayMemberPaths[i]).GetValue(item).ToString();
                var textBlock = new TextBlock()
                {
                    Text = propertyValue,
                    Width = ColumnWidths[i],
                    TextWrapping = TextWrapping.NoWrap,
                    Margin = new Thickness(2, 2, 2, 2)
                };
                content.Children.Add(textBlock);
            }
            Items.Add(content);
        }
    }
}

属性说明:

  • _itemsSource: 数据源。
  • ColumnWidths: 每列的宽度。
  • DisplayMemberPaths: 显示列的属性名称。
  • FilterMemberPath: 模糊查询的属性名称。

方法说明:

  • SetItemsSource: 设置数据源。
  • Filter: 根据输入的字符模糊查询并显示数据。

3. 在WPF界面中使用

在WPF界面中使用 MyComboBox 类,我们可以使用以下代码:

<Window x:Class="WpfApp1.MainWindow"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:MyComboBox x:Name="myComboBox" Margin="10">
            <local:MyComboBox.ColumnWidths>
                <x:Double>100</x:Double>
                <x:Double>200</x:Double>
            </local:MyComboBox.ColumnWidths>
            <local:MyComboBox.DisplayMemberPaths>
                <x:String>Id</x:String>
                <x:String>Name</x:String>
            </local:MyComboBox.DisplayMemberPaths>
        </local:MyComboBox>
    </Grid>
</Window>

4. 设置数据源和模糊查询

最后,我们可以在代码中设置数据源并根据输入的字符进行模糊查询:

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

        // 创建数据源
        var items = new List<Item>();
        for (int i = 0; i < 10000; i++)
        {
            items.Add(new Item() { Id = i, Name = 'Item ' + i });
        }

        // 设置数据源
        myComboBox.SetItemsSource(items);

        // 根据输入的字符进行模糊查询
        myComboBox.Filter('5');
    }
}

public class Item
{
    public int Id { get; set; } 
    public string Name { get; set; } 
}

完整代码示例

// MyComboBox.cs
public class MyComboBox : ComboBox
{
    // ... (属性和方法定义)
}

// MainWindow.xaml.cs
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // ... (设置数据源和模糊查询)
    }
}

// Item.cs
public class Item
{
    public int Id { get; set; } 
    public string Name { get; set; } 
}

总结

本文介绍了如何自定义一个分列下拉框,支持模糊查询和大量数据。通过代码示例和详细注释,您可以快速上手实现该功能。该自定义下拉框可以用于各种场景,例如显示大量用户信息、产品列表等。

WPF 自定义分列下拉框,支持模糊查询和大量数据

原文地址: https://www.cveoy.top/t/topic/kqKQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录