Question

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++;
        }
        else
        {
            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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top