KMC
KMC

Reputation: 20046

How to call Binding function from a method?

I have a Slider Binded to the ScaleX of a Line, like the following code:

/* Graphics on Canvas */
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;

/* declare ScaleTransformation */
ScaleTransform lRSt = new ScaleTransform();
TransformGroup lRTran = new TransformGroup();
lRTran.Children.Add(lRSt);
lR.RenderTransform = lRTran;

Binding sliderRBind1 = new Binding();
sliderRBind1.Source = sliderR;
sliderRBind1.Path = new PropertyPath("Value");
BindingOperations.SetBinding(lRSt, ScaleTransform.ScaleXProperty, sliderRBind1);
BindingOperations.SetBinding(lRSt, ScaleTransform.ScaleYProperty, sliderRBind1);


/* Slider - Slider should be placed outside the canvas to prevent being redrawn */
Slider sliderR = new Slider();
sliderR.Minimum = 1;
sliderR.Maximum = 3;
sliderR.Value = 1;
sliderR.TickPlacement = TickPlacement.BottomRight;
sliderR.TickFrequency = 0.2;
sliderR.IsSnapToTickEnabled = true;

Code works fine. But if I move the ScaleTransform to a method, the Binding is lost.

/* Graphics on Canvas */
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;

/* declare ScaleTransformation */
ScaleTransform lRSt = new ScaleTransform();
TransformGroup lRTran = new TransformGroup();
lRTran.Children.Add(lRSt);
lR.RenderTransform = lRTran;

LineSliderR(lR);

/* Slider - Slider should be placed outside the canvas to prevent being redrawn */
Slider sliderR = new Slider();
sliderR.Minimum = 1;
sliderR.Maximum = 3;
sliderR.Value = 1;
sliderR.TickPlacement = TickPlacement.BottomRight;
sliderR.TickFrequency = 0.2;
sliderR.IsSnapToTickEnabled = true; 


public void LineSliderR(Line lRSt)
{
    Binding sliderRBind1 = new Binding();
    sliderRBind1.Source = sliderR;
    sliderRBind1.Path = new PropertyPath("Value");
    BindingOperations.SetBinding(lRSt, ScaleTransform.ScaleXProperty, sliderRBind1);
    BindingOperations.SetBinding(lRSt, ScaleTransform.ScaleYProperty, sliderRBind1);

}

Why did the Binding fail if it's called to a separate method?

The two codes are (to me) identical. Both contain four same, identical, "things": a Line, a ScaleTransform, a Binding, and a Slider.

The first code has all 4 "things" in a function. The second code has the Binding call from a separate function. Both compiles without error: However, the Binding (between Line and Slider) works OK on the first code, but not the second. I am trying get the second code to work because I have many Line's and I don't want to repeat myself.

Upvotes: 1

Views: 246

Answers (1)

Edwin de Koning
Edwin de Koning

Reputation: 14387

In your second code block, inside the method LineSliderR, lRSt is an object of type Line, while in your first block it is of type ScaleTransform.

If you want your second block to behave like the first, your method LineSliderR, should accept a ScaleTransform' as parameter:

public void LineSliderR(ScaleTransform lRSt)

Upvotes: 2

Related Questions