Reputation: 200
I'd like to create a multibinding and use a converter to convert numbers.
<MultiBinding StringFormat="{}{0:F2} {1} {2:F2}">
<Binding Path="MyDouble"></Binding>
<Binding Path="MyDouble"></Binding>
<Binding Path="MyDouble" Converter="{StaticResource myConv}"></Binding>
</MultiBinding>
The first two binding are formatted properly. But the third, where I am using a custom IValueConverter to convert the displayed value, it just ignores the format string!
Does anybody know why? How could I get around it?
Please note that I don't want the converter to do the string formatting, it should just do some unit conversion (e.g. meter/feet). I plan to re-use the converter for various other controls, so the formatting stuff should be defined by the control (the xaml format string).
The complete code:
<Window x:Class="MultiBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:MultiBinding"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<src:MyConverter x:Key="myConv"></src:MyConverter>
</Window.Resources>
<Grid>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:F2} {1} {2:F2}">
<Binding Path="MyDouble"></Binding>
<Binding Path="MyDouble"></Binding>
<Binding Path="MyDouble" Converter="{StaticResource myConv}"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Window>
This is my Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MultiBinding
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class ViewModel{
double myDouble = 1.4546012347;
string myString = "hEllo";
public double MyDouble { get { return myDouble; } }
public string MyString { get { return myString; } }
}
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return 9.3423423f;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Upvotes: 2
Views: 200
Reputation: 15951
Try the following:
<MultiBinding StringFormat="{}{0:F2} {1} {2:#.##}">
...
Upvotes: 0
Reputation: 25623
I can reproduce your behavior on WPF 3.0, but not on 4.0 or 4.5. It seems converted values do not get formatted properly in 3.0.
One workaround is to do the formatting using a separate converter on the MultiBinding
itself, which will let you continue to reuse your existing value converter:
<Grid>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{src:StringFormatConverter '{}{0:F2} {1} {2:F2}'}">
<Binding Path="MyDouble" />
<Binding Path="MyDouble" />
<Binding Path="MyDouble" Converter="{StaticResource myConv}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
Example converter implementation:
public class StringFormatConverter : MarkupExtension, IMultiValueConverter
{
[ConstructorArgument("formatString")]
public string FormatString { get; set; }
public StringFormatConverter() {}
public StringFormatConverter(string formatString)
{
this.FormatString = formatString;
}
public object Convert(
object[] values,
Type targetType,
object parameter,
CultureInfo culture)
{
return string.Format(culture, this.FormatString, values);
}
public object[] ConvertBack(
object value,
Type[] targetTypes,
object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
Upvotes: 3