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

C# 我应该如何为WP7应用程序构建过滤搜索?,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我正在开发一个应用程序,它在列表框中有许多项 我想使用文本框创建搜索,这样当用户在文本框中键入内容时,列表框会过滤结果。我曾经为WPF应用程序编写过类似的功能。对于数据网格的项目,应突出显示搜索的文本。您只需要一个多值转换器,它将项目文本和搜索文本转换为新的文本块,其中包含带有突出显示部分的运行元素 转换器 转换器将文本和搜索文本转换为TextBlock实例,其中包含Run元素,用于定义样式的匹配 public class TextToHighlightedTextConverter : IMu

我正在开发一个应用程序,它在
列表框中有许多项


我想使用
文本框
创建搜索,这样当用户在
文本框
中键入内容时,
列表框
会过滤结果。

我曾经为WPF应用程序编写过类似的功能。对于
数据网格
的项目,应突出显示搜索的文本。您只需要一个
多值转换器
,它将项目文本和搜索文本转换为新的
文本块
,其中包含带有突出显示部分的
运行
元素

转换器

转换器将文本和搜索文本转换为
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;
    }
}

使用CollectionViewSource它对筛选和搜索非常有用:

参考文献:

谢谢!这篇文章对我帮助很大。我正在我的项目中尝试它。@user1538895:如果它对你有帮助的话。。请放弃投票支持答案。我不能。因为我的名声还不到15岁。我是新来的。对不起,谢谢。但我觉得CollectionViewSource会更好地满足我的需要。
<Converters:TextToHighlightedTextConverter x:Key="TextToHighlightedTextConverter">
    <Converters:TextToHighlightedTextConverter.HighlightedTextStyle>
        <Style TargetType="{x:Type Run}">
            <Setter Property="Background" Value="Orange" />
        </Style>
    </Converters:TextToHighlightedTextConverter.HighlightedTextStyle>
</Converters:TextToHighlightedTextConverter>
<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>