How can I bind RotateTransform.Angle value via a Converter?

128 Views Asked by At

I need to create a Converter that takes some value and returns another. This will be used to set the textblock RotateTransform.Angle value in XAML.

If I hard-code the value to a static number the textblock gets rotated successfully. But when it goes through the converter it does not get rotated.

Any insight would be appreciated.

Thanks.

Hard-coded value (works):

<TextBlock ...

    <RotateTransform CenterX="0.5" CenterY="0.5">
        <RotateTransform.Angle>
            10
        </RotateTransform.Angle>
    </RotateTransform>

Going through a Converter (does not work):

<TextBlock ...

    <RotateTransform CenterX="0.5" CenterY="0.5">
        <RotateTransform.Angle>
            <MultiBinding Converter="{StaticResource RelativeToAbsoluteRotationConverter}">
                <Binding Path="RelativeAngle" />
            </MultiBinding>
        </RotateTransform.Angle>
    </RotateTransform>

Converter class:

public class RelativeToAbsoluteRotationConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        // Add logic here... Function is hit, but no rotation ever takes place.

        return 10; // irrelevant
    }

    // ...
1

There are 1 best solutions below

0
Stout On

Output window:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property. Int32:'30' MultiBindingExpression:target element is 'RotateTransform' (HashCode=57454947); target property is 'Angle' (type 'Double')

The solution is to modify the object Convert() method and instead of returning an int (e.g. 10), we return a double value (e.g. 10d or 10.0).

return 10d; // This works