473,563 Members | 2,904 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.SetMyParen t(canvas1);
shpX.Width = 150;
shpX.Height = 50;
shpX.Initialize BordersAsRectan gle();
shpX.Name = "shpX";

shpX.PreviewMou seDown += new
MouseButtonEven tHandler(myElem ent_PreviewMous eDown);
shpX.PreviewMou seMove += new MouseEventHandl er(myElement_Pr eviewMouseMove) ;
shpX.MouseLeftB uttonUp += new MouseButtonEven tHandler(MouseU pHandler);

this.canvas1.Ch ildren.Add(shpX );
Canvas.SetLeft( shpX, 100);
Canvas.SetTop(s hpX, 100);

//CREATE THE 2ND CUSTOM CONTROL
myShape shpX2 = new myShape();
shpX2.SetMyPare nt(canvas1);
shpX2.Width = 150;
shpX2.Height = 50;
shpX2.Initializ eBordersAsRecta ngle();
shpX2.Name = "shpX2";

shpX2.PreviewMo useDown += new
MouseButtonEven tHandler(myElem ent_PreviewMous eDown);
shpX2.PreviewMo useMove += new MouseEventHandl er(myElement_Pr eviewMouseMove) ;
shpX2.MouseLeft ButtonUp += new MouseButtonEven tHandler(MouseU pHandler);

this.canvas1.Ch ildren.Add(shpX 2);
Canvas.SetLeft( shpX2, 300);
Canvas.SetTop(s hpX2, 300);

//CREATE THE LINE
Line ln = new Line();
ln.Stroke = Brushes.Red;
ln.StrokeThickn ess = 3;

//DATA BIND ONE END OF LINE TO THE 1ST CONTROL
Binding BndX1 = new Binding();
BndX1.Mode = BindingMode.One Way;
BndX1.Source = shpX.CenterX;
BindingOperatio ns.SetBinding(l n, Line.X1Property , BndX1);

Binding BndY1 = new Binding();
BndY1.Source = shpX.CenterY;
BindingOperatio ns.SetBinding(l n, 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(L ine.X2Property, BndX2);

Binding BndY2 = new Binding();
BndY2.Source = shpX2.CenterY;
ln.SetBinding(L ine.Y2Property, BndY2);
Can anyone please help help me get this running?

Thanks!


--
mo*******@noema il.noemail
Dec 29 '06 #1
4 2619
Hi,

"moondaddy" <mo*******@noem ail.noemailwrot e in message
news:u2******** ******@TK2MSFTN GP02.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.SetMyParen t(canvas1);
shpX.Width = 150;
shpX.Height = 50;
shpX.Initialize BordersAsRectan gle();
shpX.Name = "shpX";

shpX.PreviewMou seDown += new
MouseButtonEven tHandler(myElem ent_PreviewMous eDown);
shpX.PreviewMou seMove += new
MouseEventHandl er(myElement_Pr eviewMouseMove) ;
shpX.MouseLeftB uttonUp += new MouseButtonEven tHandler(MouseU pHandler);

this.canvas1.Ch ildren.Add(shpX );
Canvas.SetLeft( shpX, 100);
Canvas.SetTop(s hpX, 100);

//CREATE THE 2ND CUSTOM CONTROL
myShape shpX2 = new myShape();
shpX2.SetMyPare nt(canvas1);
shpX2.Width = 150;
shpX2.Height = 50;
shpX2.Initializ eBordersAsRecta ngle();
shpX2.Name = "shpX2";

shpX2.PreviewMo useDown += new
MouseButtonEven tHandler(myElem ent_PreviewMous eDown);
shpX2.PreviewMo useMove += new
MouseEventHandl er(myElement_Pr eviewMouseMove) ;
shpX2.MouseLeft ButtonUp += new MouseButtonEven tHandler(MouseU pHandler);

this.canvas1.Ch ildren.Add(shpX 2);
Canvas.SetLeft( shpX2, 300);
Canvas.SetTop(s hpX2, 300);

//CREATE THE LINE
Line ln = new Line();
ln.Stroke = Brushes.Red;
ln.StrokeThickn ess = 3;

//DATA BIND ONE END OF LINE TO THE 1ST CONTROL
Binding BndX1 = new Binding();
BndX1.Mode = BindingMode.One Way;
BndX1.Source = shpX.CenterX;
BindingOperatio ns.SetBinding(l n, Line.X1Property , BndX1);
Based on winforms experience i would try:

Binding BndX1 = new Binding();
BndX1.Source = shpX;
BndX1.Path = "CenterX";
BindingOperatio ns.SetBinding(l n, Line.X1Property , BndX1);

HTH,
Greetings
>
Binding BndY1 = new Binding();
BndY1.Source = shpX.CenterY;
BindingOperatio ns.SetBinding(l n, 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(L ine.X2Property, BndX2);

Binding BndY2 = new Binding();
BndY2.Source = shpX2.CenterY;
ln.SetBinding(L ine.Y2Property, BndY2);
Can anyone please help help me get this running?

Thanks!


--
mo*******@noema il.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("C enterX");

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.comwr ote in message
news:ck******** **************@ phobos.telenet-ops.be...
Hi,

"moondaddy" <mo*******@noem ail.noemailwrot e in message
news:u2******** ******@TK2MSFTN GP02.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.SetMyPare nt(canvas1);
shpX.Width = 150;
shpX.Height = 50;
shpX.Initializ eBordersAsRecta ngle();
shpX.Name = "shpX";

shpX.PreviewMo useDown += new
MouseButtonEve ntHandler(myEle ment_PreviewMou seDown);
shpX.PreviewMo useMove += new
MouseEventHand ler(myElement_P reviewMouseMove );
shpX.MouseLeft ButtonUp += new MouseButtonEven tHandler(MouseU pHandler);

this.canvas1.C hildren.Add(shp X);
Canvas.SetLeft (shpX, 100);
Canvas.SetTop( shpX, 100);

//CREATE THE 2ND CUSTOM CONTROL
myShape shpX2 = new myShape();
shpX2.SetMyPar ent(canvas1);
shpX2.Width = 150;
shpX2.Height = 50;
shpX2.Initiali zeBordersAsRect angle();
shpX2.Name = "shpX2";

shpX2.PreviewM ouseDown += new
MouseButtonEve ntHandler(myEle ment_PreviewMou seDown);
shpX2.PreviewM ouseMove += new
MouseEventHand ler(myElement_P reviewMouseMove );
shpX2.MouseLef tButtonUp += new MouseButtonEven tHandler(MouseU pHandler);

this.canvas1.C hildren.Add(shp X2);
Canvas.SetLeft (shpX2, 300);
Canvas.SetTop( shpX2, 300);

//CREATE THE LINE
Line ln = new Line();
ln.Stroke = Brushes.Red;
ln.StrokeThick ness = 3;

//DATA BIND ONE END OF LINE TO THE 1ST CONTROL
Binding BndX1 = new Binding();
BndX1.Mode = BindingMode.One Way;
BndX1.Source = shpX.CenterX;
BindingOperati ons.SetBinding( ln, Line.X1Property , BndX1);

Based on winforms experience i would try:

Binding BndX1 = new Binding();
BndX1.Source = shpX;
BndX1.Path = "CenterX";
BindingOperatio ns.SetBinding(l n, Line.X1Property , BndX1);

HTH,
Greetings
>>
Binding BndY1 = new Binding();
BndY1.Source = shpX.CenterY;
BindingOperati ons.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*******@noema il.noemail


Jan 4 '07 #4
Hi,

"moondaddy" <mo*******@noem ail.noemailwrot e in message
news:OY******** ******@TK2MSFTN GP04.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("C enterX");
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("Center X");
BndX1.Source = shpX;
BindingOperatio ns.SetBinding(l n, 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.comwr ote in message
news:ck******** **************@ phobos.telenet-ops.be...
>Hi,

"moondaddy" <mo*******@noem ail.noemailwrot e in message
news:u2******* *******@TK2MSFT NGP02.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.SetMyPar ent(canvas1);
shpX.Width = 150;
shpX.Height = 50;
shpX.Initiali zeBordersAsRect angle();
shpX.Name = "shpX";

shpX.PreviewM ouseDown += new
MouseButtonEv entHandler(myEl ement_PreviewMo useDown);
shpX.PreviewM ouseMove += new
MouseEventHan dler(myElement_ PreviewMouseMov e);
shpX.MouseLef tButtonUp += new MouseButtonEven tHandler(MouseU pHandler);

this.canvas1. Children.Add(sh pX);
Canvas.SetLef t(shpX, 100);
Canvas.SetTop (shpX, 100);

//CREATE THE 2ND CUSTOM CONTROL
myShape shpX2 = new myShape();
shpX2.SetMyPa rent(canvas1);
shpX2.Width = 150;
shpX2.Heigh t = 50;
shpX2.Initial izeBordersAsRec tangle();
shpX2.Name = "shpX2";

shpX2.Preview MouseDown += new
MouseButtonEv entHandler(myEl ement_PreviewMo useDown);
shpX2.Preview MouseMove += new
MouseEventHan dler(myElement_ PreviewMouseMov e);
shpX2.MouseLe ftButtonUp += new MouseButtonEven tHandler(MouseU pHandler);

this.canvas1. Children.Add(sh pX2);
Canvas.SetLef t(shpX2, 300);
Canvas.SetTop (shpX2, 300);

//CREATE THE LINE
Line ln = new Line();
ln.Stroke = Brushes.Red;
ln.StrokeThic kness = 3;

//DATA BIND ONE END OF LINE TO THE 1ST CONTROL
Binding BndX1 = new Binding();
BndX1.Mode = BindingMode.One Way;
BndX1.Sourc e = shpX.CenterX;
BindingOperat ions.SetBinding (ln, Line.X1Property , BndX1);

Based on winforms experience i would try:

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

HTH,
Greetings
>>>
Binding BndY1 = new Binding();
BndY1.Sourc e = shpX.CenterY;
BindingOperat ions.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.Sourc e = shpX2.CenterX;
ln.SetBinding (Line.X2Propert y, BndX2);

Binding BndY2 = new Binding();
BndY2.Sourc e = shpX2.CenterY;
ln.SetBinding (Line.Y2Propert y, BndY2);
Can anyone please help help me get this running?

Thanks!


--
mo*******@noema il.noemail



Jan 4 '07 #5

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

Similar topics

4
3623
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
8658
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; System.Data.DataColumn colDetail1;
3
3139
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, depending on which menu item a user selects. Each of these controls follows the same basic pattern: Get a dataset from the database and then display...
3
1847
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 automatically generated, and the objectdatasource let you design against a business object through the gui. Now I am working on my first real web application...
9
2091
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 form, have the user fill it out then put the data into a class representing a data row and then use the OLEadaptor or OLECommands to update the...
1
1428
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 code. However, since DataBinding property is gone from all controls in 2.0 and DataSourceID isn't visible among control properties, is there really no...
0
1315
by: Wayne Sepega | last post by:
I have the following Object DataSource <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetCustomer" TypeName="Customers" DataObjectTypeName="Customer" InsertMethod="InsertCustomer" OnUpdated="ObjectDataSource1_Updated" UpdateMethod="UpdateCustomer" OnSelected="ObjectDataSource1_Selected">
8
2078
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 generic type Field<T> (see sample code). public class Employee { public Field<stringForename
2
1842
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 corba datatransfer objects. The corba interface just provides business methods which take in a parameter of type java.lang.String(xml). We are...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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 we have to send another system
0
924
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.