0

So i have this simple property that changed text color with converter:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (targetType == typeof(String))
        {
            if (value.ToString().StartsWith("Start"))
            {
                // Split the string.
                string[] arr = value.ToString().Split(' ');

                // Add color attribute into value field.
                string coloredVal = string.Format("<Span Foreground=\"Yellow\">{0}</Span>", arr[3]);
                return string.Format("bla bla in {0} sec", coloredVal);
            }
        }

So i wonder how can i changed this label style and changed size, style like bold etc.

Nuz reel
  • 19
  • 3
  • 1
    I am curious, why use a converter for changing text color ? you can do a simple binding in XAML or define a style in XAML and apply it in code behind by using TryFindResource. – Dheeraj Jan 04 '16 at 16:41
  • I'd love to see an example of this approachת I have not thought about it – Nuz reel Jan 04 '16 at 16:47

1 Answers1

0

XAML isn't HTML; stuffing some markup text in there won't cause the markup text to be parsed and rendered. Incidentally, <Span> in WPF doesn't go in a Label, it goes in a TextBlock. But a TextBlock can be Label content, so it's not a big deal.

You're on the right track using a value converter, but that's not the right way to throw one at this particular case. What you want to do is conditionally apply a Style to your TextBlock, and use a value converter to check the prefix. Here's one way to do that.

XAML:

<Label
    Content="{Binding MyTextProperty}">
    <Label.Style>
        <Style 
            TargetType="Label" 
            >
            <Style.Resources>
                <local:HasPrefixValueConverter x:Key="HasPrefix" />
            </Style.Resources>
            <Style.Triggers>
                <DataTrigger 
                    Binding="{Binding MyTextProperty, Converter={StaticResource HasPrefix}, ConverterParameter='Start'}" 
                    Value="True">
                    <Setter Property="Foreground" Value="Yellow" />
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="Background" Value="DarkGreen" />
                    <!-- Etc. -->
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>

C#:

public class HasPrefixValueConverter : IValueConverter
{
    public object Convert(object value, 
        Type targetType, object parameter, 
        System.Globalization.CultureInfo culture)
    {
        if (value is String && parameter is String)
        {
            return (value as String).StartsWith(parameter as String);
        }

        return false;
    }

    public object ConvertBack(object value, 
        Type targetType, object parameter, 
        System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

If you want to have some bits of the text dynamically formatted differently than others in a TextBlock, that's a whole other ball of wax. There are ways to make that happen, but they range from ugly to gruesome.

Community
  • 1
  • 1