Friday, May 15, 2015

WPF MultiBindng Converter example


Without Converter
<TextBlock.Text>
    <MultiBinding StringFormat="{}[{0} - {1}]">
        <Binding Path="Min" FallbackValue=""/>
        <Binding Path="Max" FallbackValue=""/>
    </MultiBinding>
</TextBlock.Text>
</TextBlock>


Using Converter
public class RangeMultiValueConvertor : IMultiValueConverter
   {
       public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
       {
           if (values == nullreturn string.Empty;
           if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
           {
               return string.Empty;
           }
           return (string.Format("[{0} - {1}]", values[0], values[1]));
       }
 
       public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
       {
           return null;
       }
   }

<local:RangeMultiValueConvertor x:Key="rangeConverter"/>

<TextBlock.Text>
    <MultiBinding  Converter="{StaticResource rangeConverter}">
        <Binding Path="Min"/>
        <Binding Path="Max"/>
    </MultiBinding>
</TextBlock.Text>

No comments:

Post a Comment