Thursday, December 18, 2014

Solving WPF Binding ConverterParameter problem

Write a multiconverter instead:
<TextBlock FontSize="16" FontFamily="GE Inspira Medium" VerticalAlignment="Center" Foreground="Black">
 <TextBlock.Text >
<MultiBinding Converter="{StaticResource flowRateUnitConverter}">
<Binding Path="FlowRate"/>
<Binding Path="Unit"/>
</MultiBinding>                                   
</TextBlock.Text>
</TextBlock>


MultiConverter class
public class FlowRateUnitConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] == null || values[1] == nullreturn string.Empty;
            float? flowRate=values[0] as float?;
            string unit = values[1] as string;
            string convertedValue = values[0].ToString();
            switch(unit)
            {
                case "L/Hr":
                    convertedValue = (flowRate.Value * 60).ToString();
                    break;
                default:
                    break;
 
            }
            return convertedValue;
        }
 
        public object[] ConvertBack(
            object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

No comments:

Post a Comment