【问题标题】:Highlight text in Datagrid突出显示 Datagrid 中的文本
【发布时间】:2012-01-20 10:47:20
【问题描述】:

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

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>

谢谢!

【问题讨论】:

    标签: silverlight silverlight-4.0 datagrid highlighting


    【解决方案1】:

    您可以尝试禁用 DataGrid 的虚拟化。在this post尝试徐三的解决方案。

    希望这会有所帮助。

    米格尔

    【讨论】:

      猜你喜欢
      • 2014-05-10
      • 2019-01-28
      • 1970-01-01
      • 2013-12-20
      • 2011-06-06
      • 2011-08-31
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多