0

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
    }

    // ...
Stout
  • 463
  • 8
  • 19
  • 1
    Why do you have a multibinding with just one binding? What's the logic you need? – Andy Feb 10 '23 at 18:54
  • If it works with a regular binding I'm fine, but I read that a ConverterParameter is not a dependency property, so multibinding is the way to go: https://stackoverflow.com/questions/15309008/binding-converterparameter – Stout Feb 10 '23 at 18:59
  • @Andy: Basically the logic would 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. – Stout Feb 10 '23 at 19:01
  • 1
    What you are showing here should work. The problem seems to be somewhere else. – Clemens Feb 10 '23 at 19:01
  • 1
    If you are always converting from [0..1] to [0..360] you won't need a second parameter... – Clemens Feb 10 '23 at 19:03
  • Good news - I see a head start in the 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') – Stout Feb 10 '23 at 19:03
  • 1
    Ok, then you've got it: `return 10d;`. I would have sworn that automatic type conversion would take place after value conversion. – Clemens Feb 10 '23 at 19:04
  • 1
    That's vaguely familiar that no implicit type conversion from a converter. The irony of it itches at the back of my memory. – Andy Feb 10 '23 at 19:14

1 Answers1

0

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
Clemens
  • 123,504
  • 12
  • 155
  • 268
Stout
  • 463
  • 8
  • 19