简体   繁体   English

如何在ItemsControl中实现自定义内联搜索?

[英]How do you implement custom inline searching in an ItemsControl?

This is a two-parter. 这是两部分的。

First, in WPF the standard ListBox control automatically supports inline searching of its items. 首先,在WPF中,标准的ListBox控件自动支持对其项进行内联搜索。 It does this via using the items' ToString function, meaning if you have the focus inside a listbox and just start typing, it will do a left-most search, highlighting any items whose ToString matches what you've typed. 它通过使用项目的ToString函数来完成此操作,这意味着如果您将焦点放在列表框中并开始输入,它将进行最左侧的搜索,突出显示ToString与您键入的内容匹配的所有项目。 Subsequent key presses within a short period of time add to the search string (ie typing 'A' followed by 'S' will left-search for 'AS' whereas typign 'A' then pausing, then typing 'S' will instead left-search for 'S'. 短时间内的后续按键操作将添加到搜索字符串中(即,键入“ A”后跟“ S”将向左搜索“ AS”,而键入“ A”然后暂停,然后键入“ S”将向左搜索-搜索“ S”。

The problem is this mechanism seems to be dependent solely on the value returned by ToString, which in some cases is something we can't rely on. 问题在于这种机制似乎完全依赖于ToString返回的值,在某些情况下,这是我们不能依靠的。 Is there something else we can use instead of ToString? 还有其他可以代替ToString使用的东西吗?

The second part of this is that behavior only seems to be present in a ListBox, but no other ItemsControl objects (or hierarchical ones like a TreeView.) Without having to re-write that functionality from scratch, is there an easy way to add it to an ItemsControl? 第二部分是,行为似乎只出现在ListBox中,但是没有其他ItemsControl对象(或像TreeView这样的层次结构对象)。不必从头开始重写该功能,是否有简单的方法添加它到ItemsControl?

You can control what is searched with TextSearch.Text or TextSearch.TextPath attached properties. 您可以控制使用TextSearch.TextTextSearch.TextPath附加属性TextSearch.TextPath (See http://msdn.microsoft.com/en-us/library/system.windows.controls.textsearch(v=vs.110).aspx ) (请参阅http://msdn.microsoft.com/zh-cn/library/system.windows.controls.textsearch(v=vs.110).aspx

You can apply TextSearch.TextPath to your ListBox instance (so it searches this property instead of ToString ) or you can apply TextSearch.Text to individual ListBoxItem child (so you can set individual search text for individual elements). 您可以将TextSearch.TextPath应用于ListBox实例(以便搜索此属性而不是ToString ),也可以将TextSearch.Text应用于单个ListBoxItem子项(以便为单个元素设置单独的搜索文本)。

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <XmlDataProvider x:Key="Items" XPath="People">
            <x:XData>
                <People xmlns="">
                    <Person Name="John" Surname="Smith" />
                    <Person Name="Andrew" Surname="Johnson" />
                    <Person Name="Otis" Surname="Everett" />
                    <Person Name="Jesus" Surname="Osborn" />
                </People>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="Searches by a property (Name):" />
        <ListBox ItemsSource="{Binding Source={StaticResource Items}, XPath=*}"
                 TextSearch.TextPath="@Name">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock>
                        <Run Text="{Binding XPath=@Name}" /> <Run Text="{Binding XPath=@Surname}" />
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBlock>Searches by a individual value (number in english):</TextBlock>
        <ListBox>
            <ListBoxItem TextSearch.Text="One">1</ListBoxItem>
            <ListBoxItem TextSearch.Text="Two">2</ListBoxItem>
            <ListBoxItem TextSearch.Text="Three">3</ListBoxItem>
            <ListBoxItem TextSearch.Text="Four">4</ListBoxItem>
        </ListBox>
    </StackPanel>
</Window>

This behavior is implemented in the ItemsControl class (and you can find other examples of ItemsControl descendants with search: ComboBox , DataGrid ). 此行为在ItemsControl类中实现(您可以通过搜索找到ItemsControl后代的其他示例: ComboBoxDataGrid )。 You can set IsTextSearchEnabled property to true to make it work. 您可以将IsTextSearchEnabled属性设置为true以使其起作用。 (See http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.istextsearchenabled(v=vs.110).aspx ) (请参阅http://msdn.microsoft.com/zh-cn/library/system.windows.controls.itemscontrol.istextsearchenabled(v=vs.110).aspx

The single level search works for TreeView . 单级搜索适用于TreeView I think you should implement the search programmatically if you want to perform multi level search. 我想如果要执行多级搜索,应该以编程方式实现搜索。 (see http://social.msdn.microsoft.com/Forums/vstudio/en-US/e6d58fcc-4eaa-4bdc-8621-ce24c8efd330/treeview-textsearch ) (请参阅http://social.msdn.microsoft.com/Forums/vstudio/en-US/e6d58fcc-4eaa-4bdc-8621-ce24c8efd330/treeview-textsearch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM