【问题标题】:How to apply Search in Editable Combobox in WPF using MVVM如何使用 MVVM 在 WPF 的可编辑组合框中应用搜索
【发布时间】:2018-11-22 22:21:25
【问题描述】:

我在 WPF 中有可编辑的组合框。其中有订单号列表。 我需要在我的代码中实现以下场景。 用户可以填写起始订单号,系统在下拉列表中建议可用的关闭订单号。

谁能建议如何做到这一点?

在我的视图模型中我写过:

        public void _fillREOrderNumbers()   
        {
            List<FinishedReprintingOrderNumber> orders = _finishedProductReprintService.GetFinishedProductReprintbyOrder().ToList();
            foreach (var item in orders)
            {
                ReOrders.Add(item);
            }
        }

这是在下拉菜单中加载订单号。

查看或 XAML:

<ComboBox x:Name="cbOFab" HorizontalAlignment="Left" Margin="373,81,0,0" 
 VerticalAlignment="Top" Width="262" IsEditable="True"  
 ItemsSource="{Binding ReOrders, Mode=TwoWay}"  DisplayMemberPath="codOrder" SelectedItem="{Binding 
 ReSelectedOrder}" Background="{DynamicResource dgridRowColor}" />

到目前为止, 我可以在我的组合框中填写订单号,但我不知道如何在其中搜索。

【问题讨论】:

标签: c# wpf


【解决方案1】:

我已经以这种方式实现了对 ComboBox 中项目的过滤。

这里是 XAML:

<ComboBox
MinWidth="200"
ItemsSource="{Binding Path=Shops.View, RelativeSource={RelativeSource TemplatedParent}}" 
DisplayMemberPath="NameExtended"
SelectedItem="{Binding Path=SelectedShop, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
IsTextSearchEnabled="False"
IsEditable="True"
IsDropDownOpen="{Binding Path=ComboOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
StaysOpenOnEdit="True"
Text="{Binding Path=SearchText, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyUp">
        <i:InvokeCommandAction 
            Command="{Binding Path=FilterCommand, RelativeSource={RelativeSource TemplatedParent}}" 
        />
    </i:EventTrigger>
</i:Interaction.Triggers>

您需要 IsTextSearchEnabled 及以下的所有行。

当您在组合框中按任意键时,它会打开并过滤其中的项目,使用绑定到 ComboBox.Text 的属性 SearchText。

这是视图模型代码:

public string SearchText { get; set; }
private List<Shop> _shops;

protected void FilterShops()
    {
        ComboOpen = true;
        if (!string.IsNullOrEmpty(SearchText))
        {
            Shops.UpdateSource(_shops.Where(s => s.NameExtended.ToLower().Contains(SearchText.ToLower())));
        }
        else
        {
            Shops.UpdateSource(_shops);
        }
        OnPropertyChanged("Shops");
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    相关资源
    最近更新 更多