2

I am trying to create a DataTemplate programmarically 100% in the codebehind. Everything works perfectly, except the StringFormat on the text binding in the textblocks doesn't work.

Normally in xaml, I would accomplish this like so:

<TextBlock Text={Binding MyProperty, StringFormat=0.0} />

so I assumed I could just set the StringFormat property of the Binding object, which I did. I verified that it gets set correctly, and it does, but my view still doesn't reflect the formatting. Why?

Here is an excerpt from my code: a function that creates a DataTemplate dynamically for me. Literally everything else works perfectly, from setting the binding path to the ivalue converters, and everything. Just not the string format.

string propertyName = "myPropertyName";
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));

// the property I want is owned by myObject, which is a property of the datacontext
string bindingString = String.Format("myObject[{0}]", propertyName); 
Binding binding = new Binding(bindingString)
{
    Mode = BindingMode.OneWay,
    Converter = (IValueConverter)Application.Current.FindResource("InvalidValuesConverter"),
    StringFormat = "{0:F1}" // <-- Here is where I specify the stringFormat. I've also tried "0.0"
};

textBlock.SetBinding(TextBlock.TextProperty, binding);
12
  • 1
    MCVE is your best friend. Because as of now everything seems to be probably correct, but without actual types and format strings used and a huge pile of unrelated code it is difficult to be sure. Commented Jan 9, 2017 at 19:42
  • Edited to strip out unnecessary code Commented Jan 9, 2017 at 19:55
  • 1
    The Binding in code behind has a Converter, therefore it ignores the StringFormat. Commented Jan 9, 2017 at 19:57
  • 1
    @Clemens I tested and found that not to be the case. Commented Jan 9, 2017 at 19:59
  • @EdPlunkett Does your converter return a string? Commented Jan 9, 2017 at 20:00

1 Answer 1

2

Looks like what you're seeing is that the StringFormat is being applied, but it doesn't do numeric formatting on the string values your converter is returning. Since the particular format you're using has nothing in it but numeric formatting, in effect the converter + StringFormat processing is a no-op in the non-NaN case. The quickest way to test this assumption is to give it a format like N={0:#}, which I did. It formatted decimal 3.5 as "N=4" and string "3.5" as "N=3.5".

Naturally, values are passed through the converter before they're formatted.

Since the only purpose of your converter to substitute an empty string for Double.NaN, I'd advise that your converter only convert to string in the NaN case, and otherwise return the double value as-is. Convert returns object so that's no problem.

For simplicity, the code below assumes that you can count on value always being double.

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (double.IsNaN((double)value)) 
            ? "" 
            : value;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.