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.