4

Is it possible to bind Text and StringFormat too?

<TextBlock Text="{Binding Path=Price, StringFormat={Binding Path=DecimalPoints}}" />

DecimalPoints is constantly changing from F0 to F15. Unfortunatelly the code above doesn't compile.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
as74
  • 700
  • 4
  • 12
  • 25

3 Answers3

7

I think your best bet is definitely a converter. Then your binding would look like this:

<TextBlock.Text>
   <MultiBinding Converter="{StaticResource StringFormatConverter }">
      <Binding Path="Price"/>
      <Binding Path="DecimalPoints"/>
   </MultiBinding>
</TextBlock.Text>

Then a quick converter (you can certainly make it nicer, but this is the general idea).

    public class StringFormatConverter : IMultiValueConverter
    {
      #region IMultiValueConverter Members

      public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          double number = (double)values[0];
          string format = "f" + ((int)values[1]).ToString();
          return number.ToString(format);
      }

      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
      {
        throw new NotImplementedException();
      }

      #endregion
    }
Liz
  • 8,780
  • 2
  • 36
  • 40
  • Works perfect, Thanks. What you mean "make it nicer"? – as74 Sep 02 '13 at 23:10
  • Well, for instance I am assuming that the values being passed are a double and an int. Also I am assuming that there are two values passed (instead of say - only one). You could make the method a little more foolproof by checking the arguments first. – Liz Sep 10 '13 at 16:47
  • @Liz, could you check for my question please: http://stackoverflow.com/questions/40134370/how-to-add-textblock-text-value-from-database-object-value – Roxy'Pro Oct 20 '16 at 07:49
5

As mentioned @Sheridan, in this case, Binding will not work. But you can create a class with static strings, and refer to them in XAML. The syntax is:

<x:Static Member="prefix : typeName . staticMemberName" .../>

Below is an example:

XAML

xmlns:local="clr-namespace:YourNameSpace"
xmlns:sys="clr-namespace:System;assembly=mscorlib"

<Grid>
    <TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat={x:Static Member=local:StringFormats.DateFormat}}" 
               HorizontalAlignment="Right" />

    <TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat={x:Static Member=local:StringFormats.Time}}" />
</Grid>

Code behind

public class StringFormats 
{
    public static string DateFormat = "Date: {0:dddd}";

    public static string Time = "Time: {0:HH:mm}";
}   

For more information, please see:

x:Static Markup Extension on MSDN

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
3

No you can't... the reason is because you can only bind to a DependencyProperty of a DependencyObject and the StringFormat property of the Binding class is just a string.

Sheridan
  • 68,826
  • 24
  • 143
  • 183