none
WPF Multibinding StringFormat failing

    Question

  • Please help.  For some reason the StringFormat on the MultiBinding in my project isn't working.  Given a simple app that sums two numbers and displays the result, I'd like to format the result to two decimal places.  What happens in the app below is that A and B are formatted to two decimal places but the StringFormat completely fails on the MultiBinding.  This results in the total showing on screen with no formatting.
      
    Given a DataSource

        public class NumberSource
        {
            public double A { get { return 1.234d; } }
            public double B { get { return 4.567d; } }
        }
    And a convererter
        [ValueConversion(typeof(double), typeof(double))]
        public class SumMultiValueConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                double total = 0;
                foreach (object o in values)
                {
                    total += System.Convert.ToDouble(o);
                }
                return total;
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotSupportedException();
            }
        }
    And XAML
    <Window x:Class="FWMultiBindStringFormat.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:me="clr-namespace:FWMultiBindStringFormat">
       <Window.DataContext>
          <me:NumberSource />
       </Window.DataContext>
       <Window.Resources>
          <me:SumMultiValueConverter x:Key="SumMultiValueConverter" />
       </Window.Resources>
       <StackPanel>
          <TextBlock x:Name="A" Text="{Binding A, StringFormat={}{0:F2} }"/>
          <TextBlock x:Name="B"
                 Text="{Binding B, StringFormat={}{0:F2} }" />
          <Label x:Name="Result">
             <Label.Content>
                <MultiBinding StringFormat="{}{0:F2}"
                              Converter="{StaticResource SumMultiValueConverter}">
                   <MultiBinding.Bindings>
                      <Binding Path="A" />
                      <Binding Path="B" />
                   </MultiBinding.Bindings>
                </MultiBinding>
             </Label.Content>
          </Label>
       </StackPanel>
    </Window>
    The result shows up with more than 2 decimal places.  Any ideas?

    Thanks,
    Ron
    Tuesday, June 30, 2009 11:52 PM

Answers

  • Have you tried without the StringFormat in the MultiBinding and use the ContentStringFormat property of the Label instead?

    PS: Another option is to change your Label into a TextBlock.

    hth,
    Marcel
    • Edited by DutchMarcel Wednesday, July 01, 2009 8:58 AM Added PS
    • Marked as answer by RNEELY Wednesday, July 01, 2009 2:13 PM
    Wednesday, July 01, 2009 8:27 AM

All replies

  • Have you tried without the StringFormat in the MultiBinding and use the ContentStringFormat property of the Label instead?

    PS: Another option is to change your Label into a TextBlock.

    hth,
    Marcel
    • Edited by DutchMarcel Wednesday, July 01, 2009 8:58 AM Added PS
    • Marked as answer by RNEELY Wednesday, July 01, 2009 2:13 PM
    Wednesday, July 01, 2009 8:27 AM
  • Yes. Both those options worked. Thanks DutchMarcel.
    Best Regard,
    Ron
    Wednesday, July 01, 2009 2:04 PM