突出显示数据网格中的文本

发布于 2024-12-27 22:20:46 字数 3035 浏览 2 评论 0原文

当用户在搜索框中输入文本并单击搜索时,我有一个在数据网格中突出显示文本的实现。问题是当用户单击数据网格中的滚动条时,突出显示消失了。下面是我的用于突出显示的自定义类:

public class SearchableTextBlock
{
    public static DependencyProperty SearchPhraseProperty =
        DependencyProperty.RegisterAttached(
            "SearchPhrase",
            typeof(string),
            typeof(SearchableTextBlock), new PropertyMetadata("", SearchPhraseChanged));

    public static string GetSearchPhrase(UIElement element)
    {
        return (string)element.GetValue(SearchPhraseProperty);
    }
    public static void SetSearchPhrase(UIElement element, string value)
    {
        element.SetValue(SearchPhraseProperty, value);
    }
    public static void SearchPhraseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue == null)
            return;

        if ((d as TextBlock) != null)
        {
            TextBlock tbx = d as TextBlock;

            String text = tbx.Text;
            tbx.Inlines.Clear();

            string txtStore = "";

            //Loops throught the entire text
            for (int i = 0; i < text.Length; i++)
            {
                txtStore += text[i];
                //If search phrase is found
                if (txtStore.ToUpper().IndexOf((e.NewValue as string).ToUpper()) > -1)
                {
                    //Creates the formatting for regular text 
                    Run runRegular = new Run();
                    runRegular.Text = txtStore.Substring(0, txtStore.ToUpper().IndexOf((e.NewValue as string).ToUpper()));

                    //Creates the formatting for the found text
                    //Foreground is hardcoded to red. 
                    Run runHighlight = new Run();
                    runHighlight.Text = txtStore.Substring(txtStore.ToUpper().IndexOf((e.NewValue as string).ToUpper()), txtStore.Length - (txtStore.ToUpper().IndexOf((e.NewValue as string).ToUpper())));
                    runHighlight.Foreground = new SolidColorBrush(Colors.Red);
                    runHighlight.FontWeight = FontWeights.Bold;

                    //Inserts the formatted text to the textblock 
                    txtStore = "";
                    tbx.Inlines.Add(runRegular);
                    tbx.Inlines.Add(runHighlight);
                }
            }

            Run runRemaining = new Run();
            runRemaining.Text = txtStore;
            tbx.Inlines.Add(runRemaining);
        }
    }
}

这是我的 XAML:

<sdk:DataGridTemplateColumn x:Name="Database" >
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Database}"
                                sTextBlock:SearchableTextBlock.SearchPhrase="{Binding SDatabase, Mode=TwoWay}"/>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>

谢谢!

I have an implementation of highlight text in datagrid when the user input a text in searchbox and click on search. The problem is when the user clicks the scrollbar in datagrid, the hightlight is gone. Below is my custom class for hightlighting:

public class SearchableTextBlock
{
    public static DependencyProperty SearchPhraseProperty =
        DependencyProperty.RegisterAttached(
            "SearchPhrase",
            typeof(string),
            typeof(SearchableTextBlock), new PropertyMetadata("", SearchPhraseChanged));

    public static string GetSearchPhrase(UIElement element)
    {
        return (string)element.GetValue(SearchPhraseProperty);
    }
    public static void SetSearchPhrase(UIElement element, string value)
    {
        element.SetValue(SearchPhraseProperty, value);
    }
    public static void SearchPhraseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue == null)
            return;

        if ((d as TextBlock) != null)
        {
            TextBlock tbx = d as TextBlock;

            String text = tbx.Text;
            tbx.Inlines.Clear();

            string txtStore = "";

            //Loops throught the entire text
            for (int i = 0; i < text.Length; i++)
            {
                txtStore += text[i];
                //If search phrase is found
                if (txtStore.ToUpper().IndexOf((e.NewValue as string).ToUpper()) > -1)
                {
                    //Creates the formatting for regular text 
                    Run runRegular = new Run();
                    runRegular.Text = txtStore.Substring(0, txtStore.ToUpper().IndexOf((e.NewValue as string).ToUpper()));

                    //Creates the formatting for the found text
                    //Foreground is hardcoded to red. 
                    Run runHighlight = new Run();
                    runHighlight.Text = txtStore.Substring(txtStore.ToUpper().IndexOf((e.NewValue as string).ToUpper()), txtStore.Length - (txtStore.ToUpper().IndexOf((e.NewValue as string).ToUpper())));
                    runHighlight.Foreground = new SolidColorBrush(Colors.Red);
                    runHighlight.FontWeight = FontWeights.Bold;

                    //Inserts the formatted text to the textblock 
                    txtStore = "";
                    tbx.Inlines.Add(runRegular);
                    tbx.Inlines.Add(runHighlight);
                }
            }

            Run runRemaining = new Run();
            runRemaining.Text = txtStore;
            tbx.Inlines.Add(runRemaining);
        }
    }
}

Here's my XAML:

<sdk:DataGridTemplateColumn x:Name="Database" >
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Database}"
                                sTextBlock:SearchableTextBlock.SearchPhrase="{Binding SDatabase, Mode=TwoWay}"/>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜深人未静 2025-01-03 22:20:46

您可以尝试禁用 DataGrid 的虚拟化。尝试这篇文章中的 Xusan 解决方案。

希望这有帮助。

米格尔

You could try disable the DataGrid's virtualization. Try Xusan's solution in this post.

Hope this helps.

Miguel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文