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

2006/11/03

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.:
<TextBlock>
 <TextBlock.Text>
  <MultiBinding 
        Converter="{StaticResource StringFormatterConverter}" 
        ConverterParameter="{}{0}, {1}">
   <Binding Path="Customer.Lastname" />
   <Binding Path="Customer.Firstname" />
  </MultiBinding>
 </TextBlock.Text>
</TextBlock>

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;
  break;
 }
}

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

Happy WPFing!

7 Comments:

Post a Comment

<< Home