MVVM - 如何更改数据网格中单个字符的背景色

时间:2016-03-10 11:00:05

标签: wpf mvvm

我会将包含搜索值(搜索名称)的行显示为具有不同颜色的此值(在数据网格中)。

见下图。

关于这个的一些想法?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以通过创建一个扩展标准TextBlock的新控件来实现此目的,该控件使用一系列Run项目来显示文本,使用适当的格式。

public class HighlightTextBlock: TextBlock
{
    public string BaseText
    {
        get { return (string)GetValue(BaseTextProperty); }
        set { SetValue(BaseTextProperty, value); }
    }

    public static readonly DependencyProperty BaseTextProperty =
        DependencyProperty.Register("BaseText", typeof(string), typeof(HighlightTextBlock), new PropertyMetadata(null, UpdateDisplay));

    public string HighlightText
    {
        get { return (string)GetValue(HighlightTextProperty); }
        set { SetValue(HighlightTextProperty, value); }
    }

    public static readonly DependencyProperty HighlightTextProperty =
        DependencyProperty.Register("HighlightText", typeof(string), typeof(HighlightTextBlock), new PropertyMetadata(null, UpdateDisplay));

    public Brush HighlightBrush
    {
        get { return (Brush)GetValue(HighlightBrushProperty); }
        set { SetValue(HighlightBrushProperty, value); }
    }

    public static readonly DependencyProperty HighlightBrushProperty =
        DependencyProperty.Register("HighlightBrush", typeof(Brush), typeof(HighlightTextBlock), new PropertyMetadata(Brushes.Orange, UpdateDisplay));

    public bool HighlightCaseSensitive
    {
        get { return (bool)GetValue(HighlightCaseSensitiveProperty); }
        set { SetValue(HighlightCaseSensitiveProperty, value); }
    }

    public static readonly DependencyProperty HighlightCaseSensitiveProperty =
        DependencyProperty.Register("HighlightCaseSensitive", typeof(bool), typeof(HighlightTextBlock), new PropertyMetadata(false, UpdateDisplay));

    private static void UpdateDisplay(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var hightlightTextBlock = sender as HighlightTextBlock;

        if (hightlightTextBlock == null)
            return;

        hightlightTextBlock.Inlines.Clear();

        if (string.IsNullOrEmpty(hightlightTextBlock.BaseText))
            return;

        if (string.IsNullOrEmpty(hightlightTextBlock.HighlightText))
        {
            hightlightTextBlock.Inlines.Add(new Run(hightlightTextBlock.BaseText));
            return;
        }

        var textItems = Regex.Split(hightlightTextBlock.BaseText,
            "(" + hightlightTextBlock.HighlightText + ")",
            hightlightTextBlock.HighlightCaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase);

        foreach (var item in textItems)
        {
            var run = new Run(item);
            var highlight = hightlightTextBlock.HighlightCaseSensitive
                ? string.Compare(item, hightlightTextBlock.HighlightText, StringComparison.InvariantCulture) == 0
                : string.Compare(item, hightlightTextBlock.HighlightText, StringComparison.InvariantCultureIgnoreCase) == 0;

            if (highlight)
                run.Background = hightlightTextBlock.HighlightBrush;

            hightlightTextBlock.Inlines.Add(run);
        }
    }
}

HighlightText值周围的括号告诉Regex.Split在返回的项目列表中包含匹配的文本。

然后,可以将此控件用作datagrid列定义中的项模板的一部分。 See here有关如何执行此操作的示例。