I want to make part of the text of a textblock bold. This is what i tried in the IValueConverter but it does not seem to work.
public class Highlighter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return null;
}
return "Question1:<Bold>Answer1</Bold>, Question2:<Bold>Answer2</Bold>, Question3:<Bold>Answer3</Bold>";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
This does not make the Answer bold.
This is how i am using it in XAML.
<TextBlock Height="Auto" Width="Auto" MaxHeight="64" Text="{Binding Path=QuestionAnswer, Mode=OneWay, Converter={x:Static Highlighter}}" />
Is there a way i can achieve this by formatting the text or by sending the TextBlock to the converter?
It is definitely possible to do with
TextBlock
control, but considering all the efforts you might want to switch to other control (ItemsControl
for example).Anyway, here is a solution. There are actually several problems to solve:
TextBlock.Text
property isstring
, and you can't assign preformatted text to itTextBlock.Inlines
can accept formatted text, but it is read-only propertyInline
objects, but I don't know any)You can create an attached property to deal with the first 2 problems:
XAML would change just a bit:
Note that you'll need to map you namespace where
TextBlockEx
is declared inxmlns:local="clr-namepace:<namespace_name>"
in XAML.Now you need to construct formatted text in converter instead of plain text to solve the last problem:
Ya, something like this should put ya on track;