I am trying to databind some graphics to a slider to adjust the graphic size in code-behind.
I tried various ways but I can’t find a way to bind the the ScaleTransform’s ScaleX property in C# code. (I’m generating this in run-time code, so no XAML)
Here is my sample code, which doesn’t work
Line lR = new Line();
lR.X1 = 0;
lR.Y1 = 0;
lR.X2 = 150;
lR.Y2 = 150;
lR.Stroke = new SolidColorBrush(Colors.Blue);
lR.StrokeThickness = 2;
ScaleTransform lRSt = new ScaleTransform();
lR.RenderTransform = lRSt;
Slider sliderR = new Slider();
sliderR.Minimum = 1;
sliderR.Maximum = 3;
sliderR.Value = 1;
sliderR.TickPlacement = TickPlacement.BottomRight;
sliderR.TickFrequency = 0.2;
sliderR.IsSnapToTickEnabled = true;
/* Set Binding between Slider and Canvas Children */
Binding sliderRBind1 = new Binding();
sliderRBind1.Source = sliderRBind1;
sliderRBind1.Path = new PropertyPath("Value");
BindingOperations.SetBinding(lRSt, ScaleTransform.ScaleXProperty, sliderRBind1);
First you need to give the
ScaleTransformand theSlidera name in the XAMLIn this case a Button
Then you create a
Bindingand useBindingOperations.SetBindingto wire things toegether.Edit:
If you want to declare the slider in code too you use
.Sourceinstead of.ElementName