Skip to content

Instantly share code, notes, and snippets.

@jhalbrecht
Created June 8, 2012 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhalbrecht/2893863 to your computer and use it in GitHub Desktop.
Save jhalbrecht/2893863 to your computer and use it in GitHub Desktop.
A value converter I used for a Callisto rating
using System;
using Windows.UI.Xaml.Data;
/*
xmlns:callisto="using:Callisto.Controls"
xmlns:lm="using:yaLolFx.Models"
<Page.Resources>
<lm:ConverterRatingMultiplier x:Key="RatingMultiplier" />
</Page.Resources>
<callisto:Rating x:Name="StarRating"
ItemCount="5"
Value="{Binding AverageStarRating, Converter={StaticResource RatingMultiplier}}" />
*/
namespace yaLolFx.Models
{
public class ConverterRatingMultiplier : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return ofMap((int)value, 0, 5, 0.1f, 1);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
// grabed this from openframeworks.cc Note: openframeworks is cool! originall c++
// tried to check http://www.codecodex.com/wiki/Main_Page couldn't figure out
// what to call this range map to search for and see other examples.
// this is imperfect, but working. :-)
float ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax)
{
return ((value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment