I'm attempting to convert a value using two threshold values, the call to the database will return three variables
Double Score; Double LowerThreshold; Double HigherThreshold; If the score is lower than the lower threshold then a red image will be shown, if it's between the two thresholds then amber will be shown and if higher than the higher threshold then green will be shown.
Currently i'm using a custom ValueConverter but i'm not sure if this is the best way to do it. I merge the high and low thresholds together so that they look like this "30,50" and then parse the values from the string and work out which image to display.
This is the code i'm using for the ValueConverter
public class ScoreConverter : IValueConverter { public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is double) { if (parameter is string) { if ((parameter as string).Contains(",")) { string[] thresholds = (parameter as string).Split(','); int lowerThreshold; int upperThreshold; bool success; success = int.TryParse(thresholds[0], out lowerThreshold); if (!success) return "Error"; success = int.TryParse(thresholds[1], out upperThreshold); if (!success) return "Error"; if ((double)value < lowerThreshold) { //red return "/Red32.png"; } else if ((double)value > upperThreshold) { //green return "/Green32.png"; } else { //amber return "/Amber32.png"; } } } } return "Error"; } public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { return "Not Possible"; } } Any suggestions for a better approach would be appreciated,
Thanks
EDIT
Decided to use the following code, the server returns the following structure
public class ScoreInformation { public double Score { get; set; } public double lowerThreshold { get; set; } public double upperThreshold { get; set; } } And this is the code that converts it and displays the correct image
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is ScoreInformation) { ScoreInformation si = (ScoreInformation)value; if (si.Score < si.lowerThreshold) { return "Red32.png"; } else if (si.Score > si.upperThreshold) { //green return "Green32.png"; } else { //amber return "Orange32.png"; } } return "Grey32.png"; } Answer: 1
That is certainly one way to do it, especially if the ranges are going to change each time you use the binding, however, if you're going to re-use the same values over and over you can declare properties on your value converter and set them on initialization.
public int upperThreshold { get; set; } public int lowerThreshold { get; set; } public string lowImage { get; set; } public string middleImage { get; set; } public string highImage { get; set; } public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is double) { if ((double)value < lowerThreshold) { //red return lowImage; } else if ((double)value > upperThreshold) { //green return highImage; } else { //amber return middleImage; } } return "Error"; } public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { return "Not Possible"; } } <local:RangeToImageConverter x:Name="MyRangeConverter" upperThreshold="30" lowerThreshold="50" lowImage="/red32.png" middleImage="/amber32.png" highImage="/green32.png" /> by : viggityhttp://stackoverflow.com/users/4572
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog