#786 – Using Data Binding to Control a 2D Transform
March 28, 2013 Leave a comment
It’s common to use data binding to control the values used for 2D transformations. Below is a simple example that binds the Angle, CenterX and CenterY properties of a RotateTransform to corresponding properties in a class.
<StackPanel Margin="20,80"> <Label Content="Dwight D. Eisenhower (1890-1969)" Background="Plum" Margin="10" HorizontalAlignment="Center" Padding="20,10" FontSize="16"> <Label.LayoutTransform> <RotateTransform Angle="{Binding IkeAngle}" CenterX="{Binding IkeCenterX}" CenterY="{Binding IkeCenterY}" /> </Label.LayoutTransform> </Label> </StackPanel>
public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; // Test data IkeAngle = 25; IkeCenterX = 0.0; IkeCenterY = 0.0; } public double IkeAngle { get; set; } public double IkeCenterX { get; set; } public double IkeCenterY { get; set; } //-- INotifyPropertyChanged implementation public event PropertyChangedEventHandler PropertyChanged = delegate { }; private void RaisePropertyChanged(string prop) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }