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

Stout 286 Reputation points
2023-02-10T18:46:21.21+00:00

Hi. 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.

The logic would be be simple: The 'RelativeAngle' parameter is a value between 0 and 1. I need to map that to a new range of 0 to 360 degrees.

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
        }
    
        // ...
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
830 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Stout 286 Reputation points
    2023-02-10T19:47:51.22+00:00

    Figured it out:

    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 10, we return 10d or 10.0.

    return 10d; // This works
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.