Afnan Bashir
Afnan Bashir

Reputation: 7419

Rotate Image from Code

I want to rotate Image Control using LayoutTransform but the problem i am facing is that i can do this in XMAL but not code behind.

i am new to WPF

here is XMAL `

 <Image Grid.Column="1" Grid.Row="4" Height="155" HorizontalAlignment="Left"    
  Margin="103,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="139"  
  Source="/7SegmentLed;component/Images/Caster1.png" Grid.RowSpan="2" >
     <Image.LayoutTransform>
       <RotateTransform Angle="{Binding AngleSlider}" />
     </Image.LayoutTransform>
  </Image>

and CODE

double AngleSlider = 90.0;

image1.DataContext = AngleSlider;

i want to update it dynamically from the values i compute at back but the fact is that i donot want to change the image it is fixed and will not change

it would be great if anyone let me know what i am doing wrong

Upvotes: 0

Views: 1618

Answers (1)

CodingGorilla
CodingGorilla

Reputation: 19842

Your binding is a bit off; while you can set the DataContext to a double, your binding is going to attempt to find a property called AngleSlider on your double, which is obviously not there.

So instead, create a class with a property of type Double called AngleSlider like so:

public class MySliderDataContext : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public double AngleSlider 
    { 
       get { return _angle; }
       set 
       { 
           _angle = value;
           if(PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs("AngleSlider"));
       }
     }           
}

Create an instance of this class, and save it in a field on your Window, and then assign that instance to the DataContext:

private MySliderDataContext _sliderAngle;

image1.DataContext = _sliderAngle;

Now, when necessary you can do:

_sliderAngle.SliderAngle = 90;

And your image should rotate.

Upvotes: 2

Related Questions