Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a RichTextBox to a Slider Control in C#

I have the following XAML code that I want to perform in xaml.cs.

<RichTextBox.LayoutTransform>
    <ScaleTransform ScaleX="{Binding ElementName=mySlider, Path=Value}"
                    ScaleY="{Binding ElementName=mySlider, Path=Value}"/>
</RichTextBox.LayoutTransform>

Basically it binds the slider to the richtextbox and performs zooming.

The following is what i have attempted:

RichTextBox newtext = new RichTextBox();
ScaleTransform mytran = new ScaleTransform();
mytran.ScaleX = mySlider.Value;
mytran.ScaleY = mySlider.Value;
newtext.LayoutTransform = mytran;
like image 642
Johnny Pauly Avatar asked Aug 18 '11 20:08

Johnny Pauly


2 Answers

The following code behind is equivalent to the Xaml

//<RichTextBox.LayoutTransform>
//    <ScaleTransform ScaleX="{Binding ElementName=mySlider, Path=Value}"
//                    ScaleY="{Binding ElementName=mySlider, Path=Value}"/>
//</RichTextBox.LayoutTransform>

ScaleTransform scaleTransform = new ScaleTransform();
Binding scaleXBinding = new Binding("Value");
scaleXBinding.Source = mySlider;
Binding scaleYBinding = new Binding("Value");
scaleYBinding.Source = mySlider;
BindingOperations.SetBinding(scaleTransform,
                             ScaleTransform.ScaleXProperty,
                             scaleXBinding);
BindingOperations.SetBinding(scaleTransform,
                             ScaleTransform.ScaleYProperty,
                             scaleYBinding);

RichTextBox newText = new RichTextBox();
newText.LayoutTransform = scaleTransform;
like image 145
Fredrik Hedblad Avatar answered Sep 29 '22 05:09

Fredrik Hedblad


you did set the transform but not the binding - it will be fixed. You need to use something like

Binding scaleBinding = new Binding("Value"){ElementName="mySlider"};
BindingOperations.SetBinding(mytran, ScaleTransform.ScaleXProperty, scaleBinding);
BindingOperations.SetBinding(mytran, ScaleTransform.ScaleYProperty, scaleBinding);

to really to the same

like image 23
Random Dev Avatar answered Sep 29 '22 04:09

Random Dev