473,660 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WPF: Trouble databinding to custom Dependency Property

I have a windows WPF sample project which is starting to work pretty good.
I can drag shapes and lines, and connect a straight line to any place on a
custom rectangle shape's boarder. Now I have a custom Polyline object which
I had to create some DPs to use in a MultiBinding. However, since I'm not
strong in using custom DPs yet, I'm getting an error when binding to the
custom DP.

When you run this project:

1) Drag the right end of the Red line (this is the Line) to the rectangle
and drop it. now drag the rectangle. You can see it stays connected.

2) Now drag the right end of the Blue line (this is the Polyline) and drop
it on the rectangle. You will get the error.

3) Note, you can right click on the ends of the Polyline to add additional
line segments. its pretty cool.

Can someone please show me how to create and bind thes custom DPs?

Thanks.

Note: the attachment was 107K so I could not post it. please send me an
email go g e o r g e p At n w i s Dot n e t.
and I will email it to you.
--
mo*******@noema il.noemail

Jan 23 '07 #1
3 9542
Hi moondaddy,

To simply the scenario, I will use your previous version of code (v3) to
demonstrate the idea on how to bind a Polyline's last point to a
Rectangle's border.

First, we can create following static class to add two DPs to the Polyline
and handle the callback when the value gets changed (i.e. the data binding
returns updated data):

public static class PolylineDPs
{
public static readonly DependencyPrope rty EndPointXProper ty =
DependencyPrope rty.Register("E ndPointX", typeof(double), typeof(Polyline ),
new FrameworkProper tyMetadata(new
PropertyChanged Callback(EndPoi ntX_Changed)));
public static readonly DependencyPrope rty EndPointYProper ty =
DependencyPrope rty.Register("E ndPointY", typeof(double), typeof(Polyline ),
new FrameworkProper tyMetadata(new
PropertyChanged Callback(EndPoi ntY_Changed)));

private static void EndPointX_Chang ed(DependencyOb ject sender,
DependencyPrope rtyChangedEvent Args e)
{
Polyline pl = (Polyline)sende r;
Point p = pl.Points[pl.Points.Count - 1];
p.X = (double) (e.NewValue);
pl.Points[pl.Points.Count - 1] = p;
}

private static void EndPointY_Chang ed(DependencyOb ject sender,
DependencyPrope rtyChangedEvent Args e)
{
Polyline pl = (Polyline)sende r;
Point p = pl.Points[pl.Points.Count - 1];
p.Y = (double)(e.NewV alue);
pl.Points[pl.Points.Count - 1] = p;
}
}

After that, you can freely use the same LineBindingConv erter and
MultiBinding object to bind the Polyline's end point to the Rectangle's
border:

Polyline DrawPolyline()
{
Polyline pl = new Polyline();
pl.Stroke = Brushes.Blue;
pl.StrokeThickn ess = 3;
pl.Points.Add(n ew Point(10, 10));
pl.Points.Add(n ew Point(50, 10));
pl.Points.Add(n ew Point(150, 150));
dragCanvas.Chil dren.Add(pl);
return pl;
}

Line myLine = DrawLine();
SimpleRectangle myRect = DrawRectangle() ;
Polyline pl = DrawPolyline();

MultiBinding multiBinding = new MultiBinding();
multiBinding.Co nverter = new LineBindingConv erter();
multiBinding.Co nverterParamete r = 1.0;

Binding binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("L eft");
multiBinding.Bi ndings.Add(bind ing);

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("W idth");
multiBinding.Bi ndings.Add(bind ing);

myLine.SetBindi ng(Line.X2Prope rty, multiBinding);
pl.SetBinding(P olylineDPs.EndP ointXProperty, multiBinding);

multiBinding = new MultiBinding();
multiBinding.Co nverter = new LineBindingConv erter();
multiBinding.Co nverterParamete r = 0.3;

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("T op");
multiBinding.Bi ndings.Add(bind ing);

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("H eight");
multiBinding.Bi ndings.Add(bind ing);

myLine.SetBindi ng(Line.Y2Prope rty, multiBinding);
pl.SetBinding(P olylineDPs.EndP ointYProperty, multiBinding);
This will both bind the Line's end point and the Polyline's end point to
the same point on the right border of the Rectangle.

Hope this helps.

Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

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

Jan 24 '07 #2
Thanks Walter! As always, your samples and explanations are extremely
helpful. This is starting to work quite nicely.

Now, I need to know how to remove these bindings.

For example, here's my steps:

1) I drag the end of the polyline over the rectangle and on the
dragcompleted even I execute the code from the previous post which binds the
end of the polyline to the rectangle. Good.
2) Now I drag the end of the line off and away from the rectangle so I need
to Un-Bind the line from the rectangle.
3) Then, when I drag the rectangle, the 'EndPointY_Chan ged' event fires and
the end of the polyline jumps back to where its connected to the rectangle.

private static void EndPointY_Chang ed(DependencyOb ject sender,
DependencyPrope rtyChangedEvent Args e)
{
Polyline pl = (Polyline)sende r;
Point p = pl.Points[pl.Points.Count - 1];
p.Y = (double)(e.NewV alue);
pl.Points[pl.Points.Count - 1] = p;
}
}
I tried making the variables private at the class level so I could set them
to null after dragging the line away from the rectangle:

MultiBinding multiBinding;
Binding binding;

but that had no effect.

How do you recommend Un-Binding the line from the rectangle?

Thanks.

"Walter Wang [MSFT]" <wa****@online. microsoft.comwr ote in message
news:rv******** ******@TK2MSFTN GHUB02.phx.gbl. ..
Hi moondaddy,

To simply the scenario, I will use your previous version of code (v3) to
demonstrate the idea on how to bind a Polyline's last point to a
Rectangle's border.

First, we can create following static class to add two DPs to the Polyline
and handle the callback when the value gets changed (i.e. the data binding
returns updated data):

public static class PolylineDPs
{
public static readonly DependencyPrope rty EndPointXProper ty =
DependencyPrope rty.Register("E ndPointX", typeof(double), typeof(Polyline ),
new FrameworkProper tyMetadata(new
PropertyChanged Callback(EndPoi ntX_Changed)));
public static readonly DependencyPrope rty EndPointYProper ty =
DependencyPrope rty.Register("E ndPointY", typeof(double), typeof(Polyline ),
new FrameworkProper tyMetadata(new
PropertyChanged Callback(EndPoi ntY_Changed)));

private static void EndPointX_Chang ed(DependencyOb ject sender,
DependencyPrope rtyChangedEvent Args e)
{
Polyline pl = (Polyline)sende r;
Point p = pl.Points[pl.Points.Count - 1];
p.X = (double) (e.NewValue);
pl.Points[pl.Points.Count - 1] = p;
}

private static void EndPointY_Chang ed(DependencyOb ject sender,
DependencyPrope rtyChangedEvent Args e)
{
Polyline pl = (Polyline)sende r;
Point p = pl.Points[pl.Points.Count - 1];
p.Y = (double)(e.NewV alue);
pl.Points[pl.Points.Count - 1] = p;
}
}

After that, you can freely use the same LineBindingConv erter and
MultiBinding object to bind the Polyline's end point to the Rectangle's
border:

Polyline DrawPolyline()
{
Polyline pl = new Polyline();
pl.Stroke = Brushes.Blue;
pl.StrokeThickn ess = 3;
pl.Points.Add(n ew Point(10, 10));
pl.Points.Add(n ew Point(50, 10));
pl.Points.Add(n ew Point(150, 150));
dragCanvas.Chil dren.Add(pl);
return pl;
}

Line myLine = DrawLine();
SimpleRectangle myRect = DrawRectangle() ;
Polyline pl = DrawPolyline();

MultiBinding multiBinding = new MultiBinding();
multiBinding.Co nverter = new LineBindingConv erter();
multiBinding.Co nverterParamete r = 1.0;

Binding binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("L eft");
multiBinding.Bi ndings.Add(bind ing);

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("W idth");
multiBinding.Bi ndings.Add(bind ing);

myLine.SetBindi ng(Line.X2Prope rty, multiBinding);
pl.SetBinding(P olylineDPs.EndP ointXProperty, multiBinding);

multiBinding = new MultiBinding();
multiBinding.Co nverter = new LineBindingConv erter();
multiBinding.Co nverterParamete r = 0.3;

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("T op");
multiBinding.Bi ndings.Add(bind ing);

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("H eight");
multiBinding.Bi ndings.Add(bind ing);

myLine.SetBindi ng(Line.Y2Prope rty, multiBinding);
pl.SetBinding(P olylineDPs.EndP ointYProperty, multiBinding);
This will both bind the Line's end point and the Polyline's end point to
the same point on the right border of the Rectangle.

Hope this helps.

Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

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

Jan 24 '07 #3
You could use BindingOperatio ns.ClearAllBind ings() or ClearBinding to clear
the binding(s):

#BindingOperati ons.ClearAllBin dings Method (System.Windows .Data)
http://msdn2.microsoft.com/en-gb/lib...indingoperatio
ns.clearallbind ings.aspx

Please note: Clearing the binding removes the binding so that the value of
the dependency property is changed to whatever it would have been without
the binding. This value could be a default value, an inherited value, or a
value from a data template binding.
Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

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

Jan 25 '07 #4

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

Similar topics

8
2175
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got answered... In 1.1 we always did our own myDataAdapter.fills and we liked that control for lots of good reasons. Now the new DataSource (or is it a TableAdapter:Dataset) automatically fills the Gridview. How can we control that fill? In a...
4
2623
by: moondaddy | last post by:
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...
3
1882
by: Peter | last post by:
Hi! I am having some very strange behavior with my databound controls. It's taken a long time to isolate exactly what is provoking the problem, but I'm still leagues away from solving it. I have a DataView which filters a DataSet. Bound to this dataview is a ListBox, via its DataSource property. The DisplayMember is the name property of the row. Simple enough so far?
3
1635
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hi All, I have the following (much-simplified) situation. I have a class, part of which involves drawing graphics on a Canvas. I may have a number of instances of these graphics, which may include a line, two rectangles used as endpoints, and perhaps a label for the line. I want to somehow later associate a graphic on the canvas with an instance of the class. For instance, if the user selects an endpoint and drags it, I want to...
4
3895
by: Linda Liu[MSFT] | last post by:
Hi Moondaddy, I downloaded your sample project and run it and did see the problem on my side. There're three problems in the source code of your project. 1. You should move the following lines of code from the GetGeometry method within the CustomPolyLine class: _points.Add(new Point(10, 10));
7
10909
by: Linda Liu[MSFT] | last post by:
Hi George, I have downloaded your sample solution and built it on my machine. I got a compilation error indicating that the type of "CustomResources" doesn't exist in the following xaml code: <Expander Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:CustomResources}, ResourceId=CustomExpanderStyle1}}" >
23
4520
by: raylopez99 | last post by:
Here I am learning WinForms and two months into it I learn there's a WPF API that is coming out. Is this WPF out yet, and is it a threat to WinForms, in the sense that all the library routines I memorize and/ or familiarize myself in WinForms will disappear in WPF? I did note in one early version of C#, the non-generic "ArrayList" was replaced by the generic and template <based List, but that kind of change is not a big deal. If WPF...
2
12910
by: Rick | last post by:
First of all, is there a group more specific to WPF? I have a WPF form and I want to bind to a local member variable from the xaml. the code behind is like this: Partial Public Class MyClass Public Property MyProp as system.xml.XmlDocument get...
0
1071
by: nelsonbrodyk | last post by:
Hey All, I have been able to set up a databinding to a property with the following code: <TextBox x:Name="txtTextBox" Text="{Binding Path=NameFirst, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> and in the code: profile = context.NewProfile(); profile.NameFirst = "My Name"; txtTextBox.DataContext = profile;
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8754
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4177
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
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
2
1740
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.