如何设置转换器中值的样式?

How style the values in a converter?

我有一个 MultiValueConverter 并且需要 'values' 在应用程序中输入时加粗。下面是我的代码。我可以在后面的代码中添加一些东西来使所有值都加粗吗?谢谢

 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;
    }
}

您将无法在单个 TextBlock 内完成此操作。

最简单的解决方案是更改 XAML,以便将五个值绑定到单独的文本块,您可以单独设置样式:

<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>

另一种方法是使用 RichTextBox 并从一系列 Runs 构建文本或将 Runs 绑定到您的属性。

看了就不想做了
文本可绑定到 TextBlock,但内联不可绑定
所以你需要用转换器构建内联

考虑一个 FlowDocument 和一个 FlowDoument 查看器

或者像 ChrisF 的回答那样做

绑定到内容控件

[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","&#13;\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.");
        }
    }