wpf - 如何突出显示列表框中的匹配子字符串?

标签 wpf listbox

我有一个文本框和一个列表框用于搜索数据集合。在列表框中搜索文本时,如果在列表中的任何位置找到匹配的字符串,它应该以绿色和粗体显示。

例如。我有像这样的字符串集合
“依赖属性、自定义属性、普通属性”。如果我在搜索文本框中输入“prop”,那么所有三个带有“prop”(只有单词 Prop)的都应该是粗体并且它的颜色应该是绿色的。知道怎么做吗?

列表框中的数据使用 DataTemplate 表示。

最佳答案

我创建了一个 HighlightTextBehavior,您可以将其附加到列表项模板中的 TextBlock(您需要向项目添加对 System.Windows.Interactivity 的引用)。您将行为绑定(bind)到包含要突出显示的文本的属性,然后由它完成其余的工作。

目前,它只突出显示字符串的第一个实例。它还假定没有其他格式应用于文本。

using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace StringHighlight
{
    public class HighlightTextBehavior : Behavior<TextBlock>
    {
        public string HighlightedText
        {
            get { return (string)GetValue(HighlightedTextProperty); }
            set { SetValue(HighlightedTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HighlightedText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HighlightedTextProperty =
            DependencyProperty.Register("HighlightedText", typeof(string), typeof(HighlightTextBehavior), new UIPropertyMetadata(string.Empty, HandlePropertyChanged));

        private static void HandlePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as HighlightTextBehavior).HandlePropertyChanged();
        }

        private void HandlePropertyChanged()
        {
            if (AssociatedObject == null)
            {
                return;
            }

            var allText = GetCompleteText();

            AssociatedObject.Inlines.Clear();

            var indexOfHighlightString = allText.IndexOf(HighlightedText);

            if (indexOfHighlightString < 0)
            {
                AssociatedObject.Inlines.Add(allText);
            }
            else
            {
                AssociatedObject.Inlines.Add(allText.Substring(0, indexOfHighlightString));
                AssociatedObject.Inlines.Add(new Run() { 
                    Text = allText.Substring(indexOfHighlightString, HighlightedText.Length), 
                    Foreground = Brushes.Green,
                    FontWeight = FontWeights.Bold });
                AssociatedObject.Inlines.Add(allText.Substring(indexOfHighlightString + HighlightedText.Length));
            }
        }

        private string GetCompleteText()
        {
            var allText = AssociatedObject.Inlines.OfType<Run>().Aggregate(new StringBuilder(), (sb, run) => sb.Append(run.Text), sb => sb.ToString());
            return allText;
        }
    }
}

这是您如何使用它的示例:
    <Window x:Class="StringHighlight.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:b="clr-namespace:StringHighlight"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <x:Array x:Key="MyStrings" Type="{x:Type sys:String}">
                <sys:String>This is my first string</sys:String>
                <sys:String>Another string</sys:String>
                <sys:String>A third string, equally imaginative</sys:String>
            </x:Array>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBox x:Name="SearchText"/>

        <ListBox Grid.Row="1" ItemsSource="{StaticResource MyStrings}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Grid.Row="1" Text="{Binding}">
                        <i:Interaction.Behaviors>
                            <b:HighlightTextBehavior HighlightedText="{Binding ElementName=SearchText, Path=Text}"/>
                        </i:Interaction.Behaviors>
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

关于wpf - 如何突出显示列表框中的匹配子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2894912/

相关文章:

WPF,让垂直拉伸(stretch)按预期工作!

wpf - 更改按钮图像问题

c# - 从选定的数据绑定(bind)列表框项中获取值

list - 在 Windows phone 7 中更改 ListBox 的选定项

c# - WPF 中的 DirectX11 中的 SharpDX 2.5

wpf - 如何避免默认情况下在wpf Listbox中选择第一项?

c# - WPF 按钮中的图像在运行时不可见

wpf - 滚动查看器的子元素阻止用鼠标滚轮滚动?

ruby - 如何在 Ruby Glade/GTK 中制作多选列表框,也许使用 TreeView?

c# - 将项目集合从列表框转换为通用列表