I have a MultiValueConverter and need the 'values' to be bold when they are entered in the application. Below is my code. Is there something i can add in the code behind to make all the values bold? Thanks
class FlightConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values != null)
{
return "Outbound flight from " + values[0] + " to " + values[1] + " departing at " + values[2] +
" with " + values[3] + " in " + values[4];
}
return " ";
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
string[] values = null;
if (value != null)
return values = value.ToString().Split(' ');
return values;
}
}
You won't be able to do this within a single TextBlock
.
The simplest solution would be to change your XAML so that you're binding the five values to separate text blocks which you can style individually:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Outbound flight from " />
<TextBlock Text="{Binding value0}" FontWeight="Bold" />
<TextBlock Text=" to " />
<TextBlock Text="{Binding value1}" FontWeight="Bold" />
<TextBlock Text=" departing at " />
<TextBlock Text="{Binding value2}" FontWeight="Bold" />
<TextBlock Text=" with " />
<TextBlock Text="{Binding value3}" FontWeight="Bold" />
<TextBlock Text=" in " />
<TextBlock Text="{Binding value4}" FontWeight="Bold" />
</StackPanel>
Another way would be to use a RichTextBox
and build up the text from a series of Runs
or bind the Runs
to your properties.
Once you see it you won't want to do it
Text is bindable to a TextBlock but inlines are not bindable
So you need to build up the inlines with a converter
Consider a FlowDocument and one of the FlowDoument viewer
Or just do it like the answer from ChrisF
Bind to a Content control
[ValueConversion(typeof(string), typeof(object))]
public sealed class StringToXamlConverter : IValueConverter
{
/// <summary>
/// Converts a string containing valid XAML into WPF objects.
/// </summary>
/// <param name="value">The string to convert.</param>
/// <param name="targetType">This parameter is not used.</param>
/// <param name="parameter">This parameter is not used.</param>
/// <param name="culture">This parameter is not used.</param>
/// <returns>A WPF object.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string input = value as string;
if (!string.IsNullOrEmpty(input))
{
string escapedXml = SecurityElement.Escape(input);
string withTags = escapedXml.Replace("|~S~|", "<Run Style=\"{DynamicResource highlight}\">");
withTags = withTags.Replace("|~E~|", "</Run>");
//withTags = withTags.Replace("\r\n"," \n");
string wrappedInput = string.Format("<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TextWrapping=\"Wrap\">{0}</TextBlock>", withTags);
using (StringReader stringReader = new StringReader(wrappedInput))
{
try
{
using (XmlReader xmlReader = XmlReader.Create(stringReader))
{
return XamlReader.Load(xmlReader);
}
}
catch (Exception Ex)
{
Debug.WriteLine("StringToXamlConverter Exception " + Ex.Message);
Debug.WriteLine("input = " + input);
Debug.WriteLine("escapedXml = " + escapedXml);
Debug.WriteLine("withTags = " + withTags);
Debug.WriteLine("wrappedInput = " + wrappedInput);
if (App.StaticGabeLib.CurUserP.IsInRoleSysAdmin && false)
{
throw new Exception("StringToXamlConverter. Only sysAdmin gets this error - for other users the error is swallowed. " + input + " " + Ex.Message);
}
else
{
return input;
}
}
}
}
return null;
}
/// <summary>
/// Converts WPF framework objects into a XAML string.
/// </summary>
/// <param name="value">The WPF Famework object to convert.</param>
/// <param name="targetType">This parameter is not used.</param>
/// <param name="parameter">This parameter is not used.</param>
/// <param name="culture">This parameter is not used.</param>
/// <returns>A string containg XAML.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException("This converter cannot be used in two-way binding.");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.