Databinding not working in WPF between elements

  • Thread starter Thread starter moondaddy
  • Start date Start date
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!
 
Hi,

moondaddy said:
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);

Based on winforms experience i would try:

Binding BndX1 = new Binding();
BndX1.Source = shpX;
BndX1.Path = "CenterX";
BindingOperations.SetBinding(ln, Line.X1Property, BndX1);

HTH,
Greetings
 
Hi,

I'm not sure if I fully understand your question. Would you please send me
the working XAML version and the not-working code-behind version so that I
can debug it on my side? Thanks.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks but that didnt seem to have any effect. One line didnt compile which
I changed from:
BndX1.Path = "CenterX";
to
BndX1.Path = new PropertyPath("CenterX");

I'm going to send Walter a cleaned up sample of this in a response to his
post below if you want to take a look at it.

Thanks again.


Bart Mermuys said:
Hi,

moondaddy said:
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);

Based on winforms experience i would try:

Binding BndX1 = new Binding();
BndX1.Source = shpX;
BndX1.Path = "CenterX";
BindingOperations.SetBinding(ln, Line.X1Property, BndX1);

HTH,
Greetings
 
Hi,

moondaddy said:
Thanks but that didnt seem to have any effect. One line didnt compile
which I changed from:
BndX1.Path = "CenterX";
to
BndX1.Path = new PropertyPath("CenterX");

Yeah, sorry for that, new to WPF. I still believe the path thing is
necessairly... Most SDK doc examples setup the path in the binding ctor:

Binding BndX1 = new Binding("CenterX");
BndX1.Source = shpX;
BindingOperations.SetBinding(ln, Line.X1Property, BndX1);

Though i doubt this will make any difference from what you already tried.

You did mention that it worked when binding in XAML, that would indicate
that there's nothing wrong with your shape class, so it would be good to see
how you got it working using XAML.
I'm going to send Walter a cleaned up sample of this in a response to his
post below if you want to take a look at it.

If you want you may mail me that sample too.

Greetings
Thanks again.
 
Back
Top