473,500 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Databinding not working in WPF between elements

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!


--
mo*******@noemail.noemail
Dec 29 '06 #1
4 2611
Hi,

"moondaddy" <mo*******@noemail.noemailwrote in message
news:u2**************@TK2MSFTNGP02.phx.gbl...
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
>
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!


--
mo*******@noemail.noemail

Dec 30 '06 #2
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 2 '07 #3
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" <bm*************@hotmail.comwrote in message
news:ck**********************@phobos.telenet-ops.be...
Hi,

"moondaddy" <mo*******@noemail.noemailwrote in message
news:u2**************@TK2MSFTNGP02.phx.gbl...
>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_PreviewMouseDow n);
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_PreviewMouseDow n);
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
>>
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!


--
mo*******@noemail.noemail


Jan 4 '07 #4
Hi,

"moondaddy" <mo*******@noemail.noemailwrote in message
news:OY**************@TK2MSFTNGP04.phx.gbl...
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.
"Bart Mermuys" <bm*************@hotmail.comwrote in message
news:ck**********************@phobos.telenet-ops.be...
>Hi,

"moondaddy" <mo*******@noemail.noemailwrote in message
news:u2**************@TK2MSFTNGP02.phx.gbl...
>>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_PreviewMouseDo wn);
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_PreviewMouseDo wn);
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
>>>
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!


--
mo*******@noemail.noemail



Jan 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3611
by: CGuy | last post by:
Hi, I have an ASPX page which has a datagrid and this datagrid is bound to a Custom Collection. sample code this.DataGrid1.DataSource = UserManager.Users; this.DataGrid1.DataBind();
1
8653
by: Remy De almeida | last post by:
Hi, I am working on a test application to get databinding working. This is the code to bind the data. System.Data.DataRelation relCustOrd; System.Data.DataColumn colMaster1; ...
3
3131
by: Kevin Swanson | last post by:
I'm writing what should be a very simple app against an Oracle database. The app has a number of user controls, any one of which is loaded into a main display page using the loadControl method,...
3
1842
by: John Bailey | last post by:
When I first built a few web pages in ASP .Net 2.0, I thought it was great. The formview and detailview contorls would automatically layout the controls for you, the update methods were...
9
2079
by: Dennis | last post by:
I have tried using Databinding for my application but always seem to find it very restrictive (maybe I don't completely understand it enough). I always seem to find it much easier to display a...
1
1415
by: rb | last post by:
I'm a bit confused with this new data binding methodology - well, at least the "visual" part of it. With asp.net 1, I used be able to "mouse-my-way" around data binding instead of doing it in...
0
1303
by: Wayne Sepega | last post by:
I have the following Object DataSource <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetCustomer" TypeName="Customers" DataObjectTypeName="Customer"...
8
2071
by: Dirk | last post by:
Hello, I have a problem to use databinding with my business layer classes. My data class does not have simple properties (string, int or datetime), instead, all my properties are objects of the...
2
1834
by: udupi_mail | last post by:
Hello, Was hoping to get some feeback on the following: Current arch. is a swing client with corba services deployed on AIX. Data tranferred over the wire is XML(string) instead of traditional...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.