Martijn's blog - E-Commerce, EAI, BizTalk and .NET


WPF - Writing an IMultiValueConverter (update)

What I didn't realize in the previous blog entry regarding the subject was the fact that binding to a DependencyProperty and performing a string.Format blindly could lead to weird results, e.g.:
        Converter="{StaticResource StringFormatterConverter}" 
        ConverterParameter="{}{0}, {1}">
   <Binding Path="Customer.Lastname" />
   <Binding Path="Customer.Firstname" />

Could convert to "DependencyProperty.UnsetValue, DependencyProperty.UnsetValue" The solution is pretty simple:
bool argumentsAreNullOrEmpty = true;

// Check whether all values are null or unset.
foreach (object value in values)
 if (value != null && value != DependencyProperty.UnsetValue)
  argumentsAreNullOrEmpty = false;

// If we have all empty or null arguments, we do nothing.
if (argumentsAreNullOrEmpty)
 return Binding.DoNothing;

Happy WPFing!


Post a Comment

Links to this post:

Create a Link

<< Home