WPF Converter and NotifyOnTargetUpdated exclusive in a binding ?
- by Mathieu Garstecki
Hi,
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.