0

I have a textbox where the user enters in a number that is to be used as the angle to rotate an image. Is there a way to convert that number in xaml to negative? So if the user enters 50, I want the image to rotate -50 degrees instead of +50. I'd rather not do it in the code-behind. Code for it is below:

<TextBox x:Name="testing" />

 <Image Source="aaaa.png" x:Name="thisimage" >
    <Image.LayoutTransform>
        <RotateTransform CenterX="0.5" CenterY="0.5"  Angle="{Binding Path=Text , ElementName=testing, UpdateSourceTrigger=PropertyChanged}" />
    </Image.LayoutTransform>
 </Image>
pfinferno
  • 1,779
  • 3
  • 34
  • 62
  • 2
    Would a ValueConverter be acceptable or are you looking for XAML only solutions? A ValueConverter is code, but not "code behind" – default Feb 16 '15 at 17:52
  • Well, I'd like a XAML only solution just because I'm trying to get out of the habit of doing so much in C# with twice the amount of code. But if it's not possible or too much trouble then I could do a valueconverter. – pfinferno Feb 16 '15 at 17:57
  • 1
    But XAML is for layout and code behind is for logic. – paparazzo Feb 16 '15 at 18:07
  • 1
    there's a good thread [here](http://stackoverflow.com/questions/2580467/wpf-property-data-binding-to-negate-the-property) describing ValuConverters, and a second solution, simply `NegatedText { get { return -Text; } }` which I actually think is the easiest solution. When I think about it, I don't think there is any solution without writing *any* code. – default Feb 16 '15 at 18:09

2 Answers2

2

This would be a pure XAML solution:

<Image.LayoutTransform>
    <TransformGroup>
        <ScaleTransform ScaleX="-1"/>
        <RotateTransform CenterX="0.5" CenterY="0.5"
            Angle="{Binding Path=Text, ElementName=testing,
                            UpdateSourceTrigger=PropertyChanged}" />
        <ScaleTransform ScaleX="-1"/>
    </TransformGroup>
</Image.LayoutTransform>

I would still recommend using a binding converter.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • This is what I was looking for. The other answers/comments helped though, I'm looking into converters now. – pfinferno Feb 16 '15 at 18:25
1

Try following code.

Firstly create implementing IValueConverter:

public class AngleConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var angle = (int)value;

        if (angle != null)
        {
            return -angle;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

XAML:

<TextBox x:Name="testing" />
<Image Source="aaaa.png" x:Name="thisimage" >
<Image.LayoutTransform>
    <RotateTransform CenterX="0.5" CenterY="0.5"  Angle="{Binding Path=Text , ElementName=testing, Converter={StaticResource angleConverter} UpdateSourceTrigger=PropertyChanged}" />
</Image.LayoutTransform>

Add reference to your converter:

xmlns:cr="using:[project].Converters"

Don't forget add to resource:

<UserControl.Resources>
    <cr:AngleConverter x:Key="angleConverter"/>
</UserControl.Resources>
Yuri Dorokhov
  • 716
  • 5
  • 24
  • 1
    It seems the method is incorrectly implemented according to the [documentation](https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert%28v=vs.110%29.aspx). It shouldn't be `(object, Type, object, string)`, but `(object, Type, object, CultureInfo)` – default Feb 16 '15 at 18:12
  • 3
    @Default Depends on the platform. Here, it's the correct implementation for Windows Runtime, although the question was for WPF. The XAML namespace declaration `xmlns:cr="using:..."` is also Windows Runtime. – Clemens Feb 16 '15 at 18:13