Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ItemsSource在筛选wpf时突出显示?_C#_Wpf_Filter_Textbox_Collectionview - Fatal编程技术网

C# ItemsSource在筛选wpf时突出显示?

C# ItemsSource在筛选wpf时突出显示?,c#,wpf,filter,textbox,collectionview,C#,Wpf,Filter,Textbox,Collectionview,我有一个itemsource绑定到我的数据。我有一个TextBox,当用户开始输入它时,我会根据textBoxTextchanged事件上的以下filter谓词过滤项目: ICollectionView listView = CollectionViewSource.GetDefaultView(myControl.ItemsSource); listView.Filter = ((x) => { MyData data = x as MyData;

我有一个
itemsource
绑定到我的数据。我有一个
TextBox
,当用户开始输入它时,我会根据
textBoxText
changed事件上的以下
filter谓词
过滤项目:

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

listView.Filter = ((x) => 
{           
    MyData data = x as MyData;
    return data.Name.Contains(searchString, System.StringComparison.InvariantCultureIgnoreCase);
});
这样可以很好地过滤列表。但是,我也希望项目在键入时以黄色突出显示输入的搜索条件。我怎样才能在wpf中做到这一点? 有点像:


如果我搜索了“est”,并且项目是
森林
,森林项目会在
列表框中以黄色或任何其他颜色突出显示
est
?感谢您的建议。

我通过创建一个自定义控件-
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>

似乎在为我工作。 在本例中,我已经对匹配子字符串的颜色进行了硬编码,但是如果希望从标记中传递该属性,则可以添加单独的属性


希望这有帮助…

我通过创建一个自定义控件-
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>

似乎在为我工作。 在本例中,我已经对匹配子字符串的颜色进行了硬编码,但是如果希望从标记中传递该属性,则可以添加单独的属性


希望这对您有所帮助……

您可以使用
文本框
为项目设置模板,并绑定
SelectionStart
SelectionLength
属性。。。我很想看到解决方案。可能是查看这些链接,您可能可以使用
TextBox
为项目设置模板,并绑定
SelectionStart
SelectionLength
属性。。。我很想看到一个解决这个问题的方法。也许可以看看这些链接