Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a problem with a databinding in WPF.

When I try to use a value converter and set the NotifyOnTargetUpdated=True property to True, I get an XamlParseException with the following message:

'System.Windows.Data.BindingExpression' value cannot be assigned to property 'Contenu' of object 'View.UserControls.ShadowedText'. Value cannot be null. Parameter name: textToFormat Error at object 'System.Windows.Data.Binding' in markup file 'View.UserControls;component/saletotal.xaml' Line 363 Position 95.

The binding is pretty standard:

<my:ShadowedText Contenu="{Binding Path=Total,
                                   Converter={StaticResource CurrencyToStringConverter},
                                   NotifyOnTargetUpdated=True}"
                 TargetUpdated="MontantTotal_TargetUpdated">
</my:ShadowedText>

(Styling properties removed for conciseness)

The converter exists in the resources and works correctly when NotifyOnTargetUpdated=True is removed. Similarly, the TargetUpdated event is called and implemented correctly, and works when the converter is removed.

Note: This binding is defined in a ControlTemplate, though I don't think that is relevant to the problem.

Can anybody explain me what is happening ? Am I defining the binding wrong ? Are those features mutually exclusive (and in this case, can you explain why it is so) ?

Thanks in advance.

More info: Here is the content of the TargetUpdated handler:

private void MontantTotal_TargetUpdated(object sender, DataTransferEventArgs e)
    ShadowedText textBlock = (ShadowedText)e.TargetObject;
    double textSize = textBlock.Taille;
    double delta = 5;
    double defaultTaille = 56;
    double maxWidth = textBlock.MaxWidth;
    while (true)
        FormattedText newFormat = new FormattedText(textBlock.Contenu,
                                                    CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                                                    new Typeface("Calibri"), textSize,
                                                    (SolidColorBrush) Resources["RougeVif"]);
        if (newFormat.Width < textBlock.MaxWidth && textSize <= defaultTaille)
            if ((Math.Round(newFormat.Width) + delta) >= maxWidth || textSize == defaultTaille)
                break;
            textSize++;
            if ((Math.Round(newFormat.Width) - delta) <= maxWidth && textSize <= defaultTaille)
                break;
            textSize--;
    textBlock.Taille = textSize;

The role of the handler is to resize the control based on the length of the content. It is quite ugly but I want to have the functional part working before refactoring.

I don't know if it's just a typo but you are missing a comma in the binding. It should look like this: {Binding Path=Total, Converter={StaticResource CurrencyToStringConverter}, NotifyOnTargetUpdated=True} – Jakob Christensen Jun 16, 2010 at 12:34 It is a typo, I added line breaks in the question for readability, there is a coma between the Converter and the NotifyOnTargetUpdated parts in the real source code. Editing the question to fix this. – Mathieu Garstecki Jun 16, 2010 at 12:39 I added the code of the handler in the question. While it is overcomplicated, I don't think it does anything that might break the binding. – Mathieu Garstecki Jun 16, 2010 at 13:04

If you're getting a XamlParseException that means this error is happening during the initialization of this control.

With NotifyOnTargetUpdated=True specified, the TargetUpdated event is being raised inside your InitializeComponent call. At this point, it's incredibly doubtful you have a DataContext, so the binding will evaluate to null. Normally, there's no problem, but you are requesting an event be raised when the property is updated.

So it's hitting your event handler with a null textBlock.Contenu value, you're passing it into the first parameter of FormattedText (which is named textToFormat) and it is throwing an ArgumentNullException.

Practice some defensive programming and check your textBlock.Contenu value for null before running your code.

Two tips for future reference:

1) When receiving an exception, paste the entire stack trace, including InnerException (so call Exception.ToString). More often than not, you will see where it's happening. If not, someone on here will see where it's happening much quicker than you got your answer.

2) When receiving an exception, and you don't know where it's being thrown from (and you clearly don't, or you'd have seen it's in your code), force Visual Studio to break on all exceptions. http://msdn.microsoft.com/en-us/library/d14azbfh(VS.80).aspx

(Note that depending on your settings, the menu item they reference may or may not be there. You can also use CTRL+ALT+E to open the Exceptions dialog.)

With this set, the debugger will stop on the exact line of code throwing the Exception.

Thank you, that was exactly the problem. I wasn't expecting at all to be called during the Initialization of the control. – Mathieu Garstecki Jun 16, 2010 at 13:41

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.