我应该如何为WP7应用程序构建过滤搜索?

时间:2012-07-20 08:12:27

标签: c# silverlight windows-phone-7

我正在开发一个应用程序,其中有ListBox个项目。

我想使用TextBox创建搜索,以便当用户输入TextBoxListBox过滤结果。

2 个答案:

答案 0 :(得分:1)

我曾为WPF应用程序编写过类似的功能。应为DataGrid的项目突出显示搜索到的文本。您只需MultiValueConverter即可将商品的文字和搜索文本转换为新的TextBlock,其中包含带有突出显示部分的Run元素。

<强>转换器

转换器会将文本和搜索文本转换为TextBlock实例,其中包含具有已定义样式的匹配项的Run个元素。

public class TextToHighlightedTextConverter : IMultiValueConverter
{
    public Style HighlightedTextStyle { get; set; }

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length > 0)
        {
            if (values.Length > 1)
            {
                var text = values[0] as string;
                var searchText = values[1] as string;
                if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(searchText))
                {
                    var textParts = GetSplittedText(text, searchText);
                    var textBlock = new TextBlock();
                    foreach (string textPart in textParts)
                    {
                        textBlock.Inlines.Add(textPart.Equals(searchText, StringComparison.OrdinalIgnoreCase)
                                                    ? new Run(textPart) {Style = HighlightedTextStyle ?? new Style()}
                                                    : new Run(textPart));
                    }
                    return textBlock;
                }
            }
            return values[0];
        }
        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    private IEnumerable<string> GetSplittedText(string text, string searchText)
    {
        IList<string> textParts = new List<string>();
        if (string.IsNullOrEmpty(searchText))
        {
            if (text.Length > 0)
            {
                textParts.Add(text);
            }
        }
        else
        {
            while (text.Length > 0)
            {
                int searchIndex = text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
                if (searchIndex > -1)
                {
                    if (searchIndex > 0)
                    {
                        string textInFrontOfMatch = text.Substring(0, searchIndex);
                        textParts.Add(textInFrontOfMatch);
                    }
                    textParts.Add(text.Substring(searchIndex, searchText.Length));
                    text = text.Remove(0, searchIndex + searchText.Length);
                }
                else
                {
                    textParts.Add(text);
                    text = string.Empty;
                }
            }
        }
        return textParts;
    }
}

xaml文件中的转换器定义

在xaml文件中,您可以定义转换器并设置应该用于匹配的样式。

<Converters:TextToHighlightedTextConverter x:Key="TextToHighlightedTextConverter">
    <Converters:TextToHighlightedTextConverter.HighlightedTextStyle>
        <Style TargetType="{x:Type Run}">
            <Setter Property="Background" Value="Orange" />
        </Style>
    </Converters:TextToHighlightedTextConverter.HighlightedTextStyle>
</Converters:TextToHighlightedTextConverter>

列表框的转换器用法

您为DataTemplate的项目定义ListBox。此DataTemplate使用ContentPresenter,其内容将由定义的转换器设置。

<ListBox ItemsSource={Binding YourItemsSource}>
    <ListBox.ItemsTemplate>
        <DataTemplate>
            <ContentPresenter>
                <ContentPresenter.Content>
                    <MultiBinding Converter="{StaticResource TextToHighlightedTextConverter}">
                        <MultiBinding.Bindings>
                            <Binding />
                            <Binding Path="YourSearchTextSource" />
                        </MultiBinding.Bindings>
                    </MultiBinding>
                </ContentPresenter.Content>
            </ContentPresenter>
        </DataTemplate>
    </ListBox.ItemsTemplate>
</ListBox>

答案 1 :(得分:0)

使用CollectionViewSource对过滤和搜索非常有用:

Reffer: http://www.geoffhudik.com/tech/2010/10/14/wp7-in-app-searching-filtering.html