M
moondaddy
I'm trying to databind a dependency property in a custom control (CenterX)
to the Line.X1Property property of line. If I can correctly do this (and
the same for the 'Y' props) then when I drag my custom control the line
should follow it (stay connected) I know the dependency props in my custom
control work OK because all works OK when I do this in xaml, but when I try
to set it up at run time via c# in the codebehind, the line does not stay
connected to the custom control.
Here's the code that creates 2 instances of the custom control (myShape) and
a line. When this code runs it creates all 3 objects and correctly connects
the line to each custom control. but when I drag the controls the line
doesn't stay connected.
//CREATE THE 1ST CONTROL
myShape shpX = new myShape();
shpX.SetMyParent(canvas1);
shpX.Width = 150;
shpX.Height = 50;
shpX.InitializeBordersAsRectangle();
shpX.Name = "shpX";
shpX.PreviewMouseDown += new
MouseButtonEventHandler(myElement_PreviewMouseDown);
shpX.PreviewMouseMove += new MouseEventHandler(myElement_PreviewMouseMove);
shpX.MouseLeftButtonUp += new MouseButtonEventHandler(MouseUpHandler);
this.canvas1.Children.Add(shpX);
Canvas.SetLeft(shpX, 100);
Canvas.SetTop(shpX, 100);
//CREATE THE 2ND CUSTOM CONTROL
myShape shpX2 = new myShape();
shpX2.SetMyParent(canvas1);
shpX2.Width = 150;
shpX2.Height = 50;
shpX2.InitializeBordersAsRectangle();
shpX2.Name = "shpX2";
shpX2.PreviewMouseDown += new
MouseButtonEventHandler(myElement_PreviewMouseDown);
shpX2.PreviewMouseMove += new MouseEventHandler(myElement_PreviewMouseMove);
shpX2.MouseLeftButtonUp += new MouseButtonEventHandler(MouseUpHandler);
this.canvas1.Children.Add(shpX2);
Canvas.SetLeft(shpX2, 300);
Canvas.SetTop(shpX2, 300);
//CREATE THE LINE
Line ln = new Line();
ln.Stroke = Brushes.Red;
ln.StrokeThickness = 3;
//DATA BIND ONE END OF LINE TO THE 1ST CONTROL
Binding BndX1 = new Binding();
BndX1.Mode = BindingMode.OneWay;
BndX1.Source = shpX.CenterX;
BindingOperations.SetBinding(ln, Line.X1Property, BndX1);
Binding BndY1 = new Binding();
BndY1.Source = shpX.CenterY;
BindingOperations.SetBinding(ln, Line.Y1Property, BndY1);
//DATA BIND THE 2ND END OF THE LINE TO THE 2ND CONTROL
// I USED 'SETBINDING' IN A DIFFERENT WAY HERE TO SEE IF IT MADE A
DIFFERENCE.
// BOTH WAYS WORKED THE SAME.
Binding BndX2 = new Binding();
BndX2.Source = shpX2.CenterX;
ln.SetBinding(Line.X2Property, BndX2);
Binding BndY2 = new Binding();
BndY2.Source = shpX2.CenterY;
ln.SetBinding(Line.Y2Property, BndY2);
Can anyone please help help me get this running?
Thanks!
to the Line.X1Property property of line. If I can correctly do this (and
the same for the 'Y' props) then when I drag my custom control the line
should follow it (stay connected) I know the dependency props in my custom
control work OK because all works OK when I do this in xaml, but when I try
to set it up at run time via c# in the codebehind, the line does not stay
connected to the custom control.
Here's the code that creates 2 instances of the custom control (myShape) and
a line. When this code runs it creates all 3 objects and correctly connects
the line to each custom control. but when I drag the controls the line
doesn't stay connected.
//CREATE THE 1ST CONTROL
myShape shpX = new myShape();
shpX.SetMyParent(canvas1);
shpX.Width = 150;
shpX.Height = 50;
shpX.InitializeBordersAsRectangle();
shpX.Name = "shpX";
shpX.PreviewMouseDown += new
MouseButtonEventHandler(myElement_PreviewMouseDown);
shpX.PreviewMouseMove += new MouseEventHandler(myElement_PreviewMouseMove);
shpX.MouseLeftButtonUp += new MouseButtonEventHandler(MouseUpHandler);
this.canvas1.Children.Add(shpX);
Canvas.SetLeft(shpX, 100);
Canvas.SetTop(shpX, 100);
//CREATE THE 2ND CUSTOM CONTROL
myShape shpX2 = new myShape();
shpX2.SetMyParent(canvas1);
shpX2.Width = 150;
shpX2.Height = 50;
shpX2.InitializeBordersAsRectangle();
shpX2.Name = "shpX2";
shpX2.PreviewMouseDown += new
MouseButtonEventHandler(myElement_PreviewMouseDown);
shpX2.PreviewMouseMove += new MouseEventHandler(myElement_PreviewMouseMove);
shpX2.MouseLeftButtonUp += new MouseButtonEventHandler(MouseUpHandler);
this.canvas1.Children.Add(shpX2);
Canvas.SetLeft(shpX2, 300);
Canvas.SetTop(shpX2, 300);
//CREATE THE LINE
Line ln = new Line();
ln.Stroke = Brushes.Red;
ln.StrokeThickness = 3;
//DATA BIND ONE END OF LINE TO THE 1ST CONTROL
Binding BndX1 = new Binding();
BndX1.Mode = BindingMode.OneWay;
BndX1.Source = shpX.CenterX;
BindingOperations.SetBinding(ln, Line.X1Property, BndX1);
Binding BndY1 = new Binding();
BndY1.Source = shpX.CenterY;
BindingOperations.SetBinding(ln, Line.Y1Property, BndY1);
//DATA BIND THE 2ND END OF THE LINE TO THE 2ND CONTROL
// I USED 'SETBINDING' IN A DIFFERENT WAY HERE TO SEE IF IT MADE A
DIFFERENCE.
// BOTH WAYS WORKED THE SAME.
Binding BndX2 = new Binding();
BndX2.Source = shpX2.CenterX;
ln.SetBinding(Line.X2Property, BndX2);
Binding BndY2 = new Binding();
BndY2.Source = shpX2.CenterY;
ln.SetBinding(Line.Y2Property, BndY2);
Can anyone please help help me get this running?
Thanks!