【问题标题】:Changing foreground color of certain string in a label更改标签中某些字符串的前景色
【发布时间】:2016-03-18 10:22:11
【问题描述】:

我想知道是否可以在以下编码中更改字符串 item.Name 的前景色,而字符串的其余部分保持我在 XAML 中标签的前景色设置中设置的默认颜色。

lblLoggedInUser.Content = "Logged in:  " + item.Name + " " + item.Surname;

我希望item.Name 的颜色与字符串的其余部分不同。这可能吗?

【问题讨论】:

  • 您是否尝试过使用带有包含不同前景属性的标签的 StackPanel?

标签: c# wpf colors


【解决方案1】:

为此,我创建了一个带有附加属性的辅助类。

public class HighlightHelper : DependencyObject
{
    public static Brush GetHighlightBrush(DependencyObject obj)
    {
        return (Brush)obj.GetValue(HighlightBrushProperty);
    }
    public static void SetHighlightBrush(DependencyObject obj, Brush value)
    {
        obj.SetValue(HighlightBrushProperty, value);
    }
    public static readonly DependencyProperty HighlightBrushProperty =
        DependencyProperty.RegisterAttached("HighlightBrush", typeof(Brush), typeof(HighlightHelper), new PropertyMetadata(Brushes.Black));

    public static string GetHighlightWord(UIElement element)
    {
        return (string)element.GetValue(HighlightWordProperty);
    }
    public static void SetHighlightWord(UIElement element, string value)
    {
        element.SetValue(HighlightWordProperty, value);
    }
    public static readonly DependencyProperty HighlightWordProperty =
        DependencyProperty.RegisterAttached("HighlightWord", typeof(string), typeof(HighlightHelper), new PropertyMetadata(OnHighlightWordChanged));

    private static void OnHighlightWordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock textBlock = (TextBlock)d;
        string text = textBlock.Text;
        string highlightWord = (string)e.NewValue;

        textBlock.Inlines.Clear();

        string[] tokens = Regex.Split(text, "(" + Regex.Escape(highlightWord) + ")");

        foreach (string token in tokens)
        {
            Run run = new Run { Text = token };

            if (token.Equals(highlightWord))
            {
                Brush highlightBrush = (Brush)textBlock.GetValue(HighlightBrushProperty);
                run.Foreground = highlightBrush;
            }

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

你可以这样使用它:

<TextBlock Text="{Binding DisplayText}" Foreground="Red"
            local:HighlightHelper.HighlightWord="{Binding TextToHighlight}"
            local:HighlightHelper.HighlightBrush="Blue"></TextBlock>

【讨论】:

    【解决方案2】:

    我认为一种方法是使用水平的StackPanel,其中包含Labels,并将Foreground 属性设置为不同的颜色。例如:

    <StackPanel>
      <StackPanel Orientation="Horizontal">
        <Label VerticalAlignment="Center" Foreground="#000000">Logged in: </Label>
        <Label VerticalAlignment="Center" Foreground="#FFFFFF"></Label>
      </StackPanel>
    </StackPanel>
    

    【讨论】:

      【解决方案3】:

      你可以使用这样的东西:

          <TextBlock>
              <Run Text="Logged in" />
              <Run Text="{Binding Name}" Foreground="Blue" />
              <Run Text="{Binding Surname}" />
          </TextBlock>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-15
        • 2012-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-30
        • 2014-01-24
        相关资源
        最近更新 更多