I'm trying to write a control that has three related properties: value, minimum and maximum. Value changes, but minimum and maximum are fixed.
So I wrote a IMultiValueConverter:
public class AngleMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double value = (double)values[0];
double minimum = (double)values[1];
double maximum = (double)values[2];
double angle = -150.0 + ((value - minimum) * 300.0 / (maximum - minimum));
return angle;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
The XAML looks like the following (it's a custom control, hence the RelativeSource stuff):
<RotateTransform.Angle>
<MultiBinding Converter="{StaticResource angleMultiConverter}">
<Binding Path="Value" RelativeSource="{RelativeSource AncestorType={x:Type local:KnobControl}}"/>
<Binding Path="MinimumValue" RelativeSource="{RelativeSource AncestorType={x:Type local:KnobControl}}" Mode="OneWay"/>
<Binding Path="MaximumValue" RelativeSource="{RelativeSource AncestorType={x:Type local:KnobControl}}" Mode="OneWay"/>
</MultiBinding>
</RotateTransform.Angle>
That works fine, but what about ConvertBack? I have to pass back three values... but I only have one? The other two are constants, so how do I know what they are?
My other solution that works is to create a dependancy property called Angle which does all the work in a custom control... but exposes Angle which seems wrong.
The only answer I've found is create a property called Angle and let that do the work, as I originally pointed out in my question, and bonyjoe concluded.