【问题标题】:Searching in WPF comboBox showing two columns在显示两列的 WPF 组合框中搜索
【发布时间】:2013-11-08 12:21:02
【问题描述】:

我必须在 WPF 组合框中启用搜索,在我的 WPF MVVM 应用程序中显示两列。

下面是我的代码,它显示了两列,例如:名字 - 姓氏

    <ComboBox Grid.Column="3" Grid.Row="15" Height="Auto" HorizontalAlignment="Stretch" 
                Name="cmbName" VerticalAlignment="Stretch"
                SelectedItem="{Binding Name, Mode=TwoWay}"
                ItemsSource="{Binding GetAllName}"
                IsTextSearchEnabled="True">

        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock DataContext="{Binding}">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} - {1}">
                                <Binding Path="FirstName" />
                                <Binding Path="LastName" />
                            </MultiBinding>
                        </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

我认为在这种情况下 IsTextSearchEnabled 没有发挥任何作用。

对此有什么帮助吗?

【问题讨论】:

  • "我必须启用搜索" - 你想实现什么搜索功能?您是否希望根据组合框中的选择过滤 ListView 或其他控件中的数据?

标签: c# wpf mvvm combobox


【解决方案1】:

您似乎有些困惑。首先,您没有ComboBox 中显示两列,而是显示了两个字段值...也许您应该编辑误导性标题?

其次,我不相信您完全了解IsTextSearchEnabled 属性的用法。但是,您是正确的,使用此属性一无所获。来自 MSDN 上的ItemsControl.IsTextSearchEnabled Property 页面:

获取或设置一个值,该值指示是否在 ItemsControl 实例上启用 TextSearch。

来自 MSDN 上的TextSearch Class 页面:

此类用于将字符串分配给控件集合中的项目。为集合中的每个项目分配一个字符串可以实现两个目标。它指定选择项目时要显示的文本,它使用户能够通过键入分配的字符串来选择项目。

例如,假设 ComboBox 包含 Image 对象的集合,其中一个是狗的图像。如果您将字符串“Dog”分配给该项目,则用户可以通过在组合框的文本框中键入单词来选择狗。一旦用户键入足够多的单词以将其与选择中的其他项目区分开来,就会选择狗的图像。如果 ComboBox 上的 IsEditable 设置为 true,则“Dog”将出现在文本框中。

您可以通过使用控件上的 TextSearch.TextPath 属性或通过设置控件集合中每个项目的 Text 属性来指定标识项目的文本。设置这些属性之一可确保不显示意外文本。如果在控件的集合项上设置 Text 属性,则 TextPath 属性将被忽略。如果将 TextPath 属性设置为不是实际属性名称的值,则 TextPath 将被忽略。

【讨论】:

    【解决方案2】:

    在这种情况下,您可以使用 TextSearch.TextPath。

    <ComboBox Grid.Column="3" Grid.Row="15" Height="Auto" HorizontalAlignment="Stretch" 
                    Name="cmbName" VerticalAlignment="Stretch"
                    SelectedItem="{Binding Name, Mode=TwoWay}"
                    ItemsSource="{Binding GetAllName}">
            <TextSearch.TextPath>FirstName</TextSearch.TextPath>
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock DataContext="{Binding}">
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0} - {1}">
                                    <Binding Path="FirstName" />
                                    <Binding Path="LastName" />
                                </MultiBinding>
                            </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    

    【讨论】:

    • 这就是我要找的东西。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多