question

BitSmithy-4663 avatar image
0 Votes"
BitSmithy-4663 asked BitSmithy-4663 commented

How to set binding, code behind, to UIElement.RotateTransform.Angle

Hello,

I am trying set binding, code behind, to UIElement.RotateTransform.Angle.

I have TextBlock (as my UIElement), now I am trying to bind its RotateTransform.Angle to
TextBox.Text Property.

I must to do it code behind (not in XAML)

Result shuold be such that in TextBox I can see TextBlock RotateTransform.Angle and If I change the angle in TextBox, TextBlock rotates.

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AryaDing-MSFT avatar image
0 Votes"
AryaDing-MSFT answered BitSmithy-4663 commented

Hi,

Welcome to Microsoft Q&A!

You could use a variable named ExtraAngle to pass the value of TextBox.Text to the behind, then use BindingOperations.SetBinding() method to set binding for the RotateTransform.AngleProperty of TextBlock.RenderTransform. Please refer to the following code.
Xaml code:

 <Grid>  
             <StackPanel>
                 <TextBlock x:Name="myTextBlock" Text="It is a good day***" Width="100" />                 
                 <TextBox Text="{x:Bind ExtraAngle,Mode=TwoWay}" />
             </StackPanel>           
 </Grid>

Code behind:

 public sealed partial class MainPage : Page,INotifyPropertyChanged
     {
         private string _ExtraAngle;
    
         public event PropertyChangedEventHandler PropertyChanged;
    
         public string ExtraAngle
         {
             get { return _ExtraAngle; }
             set
             {
                 _ExtraAngle = value;
                 RaisePropertyChanged("ExtraAngle");
                    
             }
    
         }
    
         public void RaisePropertyChanged(string propertyname=null)
         {
             PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyname));
         }
         public MainPage()
         {
             this.InitializeComponent();
             RotateTransform rotate = new RotateTransform();
             rotate.CenterX = 50;
             rotate.CenterY = 60;
    
             Binding binding = new Binding();
             binding.Source = this;
             binding.Path = new PropertyPath("ExtraAngle");
             binding.Mode = BindingMode.TwoWay;
    
             BindingOperations.SetBinding(rotate, RotateTransform.AngleProperty, binding);
             myTextBlock.RenderTransform = rotate;
         }
 }



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I did my task in other way, because, as I mentioned in my question I had to use only code behind.

This is my code:

TextBlock tbl = new TextBlock()

if(tbl.RenderTransform is RotateTransform rt)
{
Binding bRotation = new Binding() { Path = new PropertyPath("Angle") };
bRotation.Source = rt;
bRotation.Mode = BindingMode.TwoWay;

                 bRotation.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
                 tbxSelectedUiElementRotation.SetBinding(TextBox.TextProperty, bRotation);
             }
             else
             {
               //.some code here
             }

but your answer helped, so I Accept is as answer.


0 Votes 0 ·