ItemsSource在过滤WPF时突出显示?

ItemsSource highlight when filtering wpf?
2021-09-14
  •  译文(汉语)
  •  原文(英语)

我对ItemsSource数据有约束力.我有一个TextBox是用户开始打字,我筛选基于以下项目Filter predicatetextBoxText更改事件:

ICollectionView listView = CollectionViewSource.GetDefaultView(myControl.ItemsSource);

listView.Filter = ((x) => 
{           
    MyData data = x as MyData;
    return data.Name.Contains(searchString, System.StringComparison.InvariantCultureIgnoreCase);
});

这可以正常工作并筛选列表.但是,我还希望这些项目在键入时以黄色突出显示输入的搜索条件.我如何在WPF中做到这一点?就像:

如果我搜索"est",则该项目为Forest"森林"项目est以黄色突出显示为黄色或其他任何颜色ListBox?感谢您的任何建议.

速聊1:
您可能可以使用来对项目进行模板化,TextBox并绑定SelectionStartSelectionLength属性...我很希望看到对此的解决方案.
速聊2:
可能会被这些链接视为underground.infovark.com/2011/03/03/…
解决过程1

我通过创建一个自定义控件实现了这一点-HighlightableTextBlock该控件继承TextBlock并添加了以下依赖项属性:

    public string HighlightSource
    {
        get { return (string)GetValue(HighlightSourceProperty); }
        set { SetValue(HighlightSourceProperty, value); }
    }

    public static readonly DependencyProperty HighlightSourceProperty =
        DependencyProperty.Register("HighlightSource", typeof(string), typeof(HighlightableTextBlock), new PropertyMetadata("", OnChange));

OnChange事件处理程序中执行实际的突出显示:

    static void OnChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = d as HighlightableTextBlock;
        var text = textBlock.Text;
        var source = textBlock.HighlightSource;

        if (!string.IsNullOrWhiteSpace(source) && !string.IsNullOrWhiteSpace(text))
        {
            var index = text.IndexOf(source);
            if (index >= 0)
            {
                var start = text.Substring(0, index);
                var match = text.Substring(index, source.Length);
                var end = text.Substring(index + source.Length);

                textBlock.Inlines.Clear();
                textBlock.Inlines.Add(new Run(start));
                textBlock.Inlines.Add(new Run(match) { Foreground = Brushes.Red });
                textBlock.Inlines.Add(new Run(end));
            }
        }
    }

事情的标记面看起来像这样:

<controls:HighlightableTextBlock Text="{Binding SomeText}"
                                 HighlightSource="{Binding ElementName=SearchTextBox, Path=Text}">
</controls:HighlightableTextBlock>

似乎为我工作.在此示例中,我已经对匹配的子字符串的颜色进行了硬编码,但是如果您还想从标记中传递该属性,则可以添加一个单独的属性.

希望这可以帮助...

I have an ItemsSource bound up to my data. I have a TextBox that as the user starts typing it in, I filter the items based on the following Filter predicate on the textBoxText changed event:

ICollectionView listView = CollectionViewSource.GetDefaultView(myControl.ItemsSource);

listView.Filter = ((x) => 
{           
    MyData data = x as MyData;
    return data.Name.Contains(searchString, System.StringComparison.InvariantCultureIgnoreCase);
});

This works fine and filters the list. However I also want the items to highlight in yellow the entered search criteria as they type. How can I do that in wpf? Kind of like:

If i searched for "est" and the item is Forest The Forest item highlights est in yellow or any other color in the ListBox? Thanks for any suggestions.

Talk1:
You could probably template the items with a TextBox and bind the SelectionStart and SelectionLength properties... I would be interested in seeing a solution to this.
Talk2:
May be look into these links underground.infovark.com/2011/03/03/…
Solutions1

I achieved this by creating a custom control - HighlightableTextBlock that inherits from TextBlock and adds the following dependency property:

    public string HighlightSource
    {
        get { return (string)GetValue(HighlightSourceProperty); }
        set { SetValue(HighlightSourceProperty, value); }
    }

    public static readonly DependencyProperty HighlightSourceProperty =
        DependencyProperty.Register("HighlightSource", typeof(string), typeof(HighlightableTextBlock), new PropertyMetadata("", OnChange));

Performing the actual highlighting in the OnChange event handler:

    static void OnChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = d as HighlightableTextBlock;
        var text = textBlock.Text;
        var source = textBlock.HighlightSource;

        if (!string.IsNullOrWhiteSpace(source) && !string.IsNullOrWhiteSpace(text))
        {
            var index = text.IndexOf(source);
            if (index >= 0)
            {
                var start = text.Substring(0, index);
                var match = text.Substring(index, source.Length);
                var end = text.Substring(index + source.Length);

                textBlock.Inlines.Clear();
                textBlock.Inlines.Add(new Run(start));
                textBlock.Inlines.Add(new Run(match) { Foreground = Brushes.Red });
                textBlock.Inlines.Add(new Run(end));
            }
        }
    }

And the markup side of things looks like this:

<controls:HighlightableTextBlock Text="{Binding SomeText}"
                                 HighlightSource="{Binding ElementName=SearchTextBox, Path=Text}">
</controls:HighlightableTextBlock>

Seems to be working for me. I've hardcoded the color of the matched substring in this example, but you can add a separate property if you want to pass that from the markup as well.

Hope this helps...

转载于:https://stackoverflow.com/questions/15576653/itemssource-highlight-when-filtering-wpf

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入