如何设计转换器中的值?

时间:2016-01-26 11:22:20

标签: wpf styling

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

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

2 个答案:

答案 0 :(得分:2)

您将无法在一个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绑定到您的媒体资源。

答案 1 :(得分:1)

一旦你看到它,你就不会想要这样做 文本可绑定到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.");
        }
    }