Sunday, June 10, 2012

ValueConvertor & MultivalueConvertors in WPF with Example

Value convertor code and usage


usage

Multivalue convertor code and usage


/// <summary>/// First one should be Result and the next value should be isWarning/// </summary>public class BoolToImageURLConverter : IMultiValueConverter{

{


{



{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)string imagepath = string.Empty; if (values != null && values.Length == 2)bool? resultValue = values[0] as bool?;bool? isWarning = values[1] as bool?;if (resultValue.HasValue & isWarning.HasValue)//TODO:move the path to Resources// or try how to change style of the image to change the image itself
imagepath = (resultValue ==


imagepath =
}
true ?"../Images/bullet_ball_glass_green.png" : "../Images/bullet_ball_glass_red.png");if (resultValue == true && isWarning == true)"../Images/bullet_ball_glass_yellow.png";else
{
imagepath =
}
}
"../Images/bullet_ball_glass_grey.png";else
{
imagepath =
}

}

{

}
}
"../Images/bullet_ball_glass_grey.png";return new System.Windows.Media.Imaging.BitmapImage(new Uri(imagepath,UriKind.Relative));public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)return null;


usage

<Window.Resources><!--ValueConvertors-->
</Window.Resources>
...
<Image Width="20" Height="20" ><Image.Source><MultiBinding Converter="{StaticResource BoolToImageURLConverter}"><!--ConverterParameter="parameter value" goes inside MultiBinding tag--><Binding Path="Result" /><Binding Path="IsWarning" /></MultiBinding></Image.Source></Image>
<cmn:BoolToImageURLConverter x:Key="BoolToImageURLConverter"/>
 
<Window.Resources><!--ValueConvertors--><cmn:BoolToImageURLConverter x:Key="BoolToImageURLConverter"/></Window.Resources> .......
<Image Source="{Binding Path=Result, Converter={StaticResource BoolToImageURLConverter}}" Width="20" Height="20" />

public class BoolToImageURLConverter : IValueConverter{


{



{
public object Convert(object value, Type targetType,object parameter, CultureInfo culture)string imagepath;bool? boolValue = value as bool?;if (boolValue.HasValue)//TODO:move the path to Resources// or try how to change style of the image to change the image itselfimagepath = boolValue ==


}
true ?"../Images/bullet_ball_glass_green.png" :"../Images/bullet_ball_glass_red.png";else{
imagepath =
}

}


{

}
}
"../Images/bullet_ball_glass_grey.png";return imagepath;public object ConvertBack(object value, Type targetType,object parameter, CultureInfo culture)return null;

No comments:

Post a Comment

Devops links

  Build Versioning in Azure DevOps Pipelines