473,468 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Create 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*******@noemail.noemail

Jan 23 '07 #1
3 9517
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 DependencyProperty EndPointXProperty =
DependencyProperty.Register("EndPointX", typeof(double), typeof(Polyline),
new FrameworkPropertyMetadata(new
PropertyChangedCallback(EndPointX_Changed)));
public static readonly DependencyProperty EndPointYProperty =
DependencyProperty.Register("EndPointY", typeof(double), typeof(Polyline),
new FrameworkPropertyMetadata(new
PropertyChangedCallback(EndPointY_Changed)));

private static void EndPointX_Changed(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
Polyline pl = (Polyline)sender;
Point p = pl.Points[pl.Points.Count - 1];
p.X = (double) (e.NewValue);
pl.Points[pl.Points.Count - 1] = p;
}

private static void EndPointY_Changed(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
Polyline pl = (Polyline)sender;
Point p = pl.Points[pl.Points.Count - 1];
p.Y = (double)(e.NewValue);
pl.Points[pl.Points.Count - 1] = p;
}
}

After that, you can freely use the same LineBindingConverter 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.StrokeThickness = 3;
pl.Points.Add(new Point(10, 10));
pl.Points.Add(new Point(50, 10));
pl.Points.Add(new Point(150, 150));
dragCanvas.Children.Add(pl);
return pl;
}

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

MultiBinding multiBinding = new MultiBinding();
multiBinding.Converter = new LineBindingConverter();
multiBinding.ConverterParameter = 1.0;

Binding binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("Left");
multiBinding.Bindings.Add(binding);

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("Width");
multiBinding.Bindings.Add(binding);

myLine.SetBinding(Line.X2Property, multiBinding);
pl.SetBinding(PolylineDPs.EndPointXProperty, multiBinding);

multiBinding = new MultiBinding();
multiBinding.Converter = new LineBindingConverter();
multiBinding.ConverterParameter = 0.3;

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("Top");
multiBinding.Bindings.Add(binding);

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("Height");
multiBinding.Bindings.Add(binding);

myLine.SetBinding(Line.Y2Property, multiBinding);
pl.SetBinding(PolylineDPs.EndPointYProperty, 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_Changed' event fires and
the end of the polyline jumps back to where its connected to the rectangle.

private static void EndPointY_Changed(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
Polyline pl = (Polyline)sender;
Point p = pl.Points[pl.Points.Count - 1];
p.Y = (double)(e.NewValue);
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.comwrote in message
news:rv**************@TK2MSFTNGHUB02.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 DependencyProperty EndPointXProperty =
DependencyProperty.Register("EndPointX", typeof(double), typeof(Polyline),
new FrameworkPropertyMetadata(new
PropertyChangedCallback(EndPointX_Changed)));
public static readonly DependencyProperty EndPointYProperty =
DependencyProperty.Register("EndPointY", typeof(double), typeof(Polyline),
new FrameworkPropertyMetadata(new
PropertyChangedCallback(EndPointY_Changed)));

private static void EndPointX_Changed(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
Polyline pl = (Polyline)sender;
Point p = pl.Points[pl.Points.Count - 1];
p.X = (double) (e.NewValue);
pl.Points[pl.Points.Count - 1] = p;
}

private static void EndPointY_Changed(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
Polyline pl = (Polyline)sender;
Point p = pl.Points[pl.Points.Count - 1];
p.Y = (double)(e.NewValue);
pl.Points[pl.Points.Count - 1] = p;
}
}

After that, you can freely use the same LineBindingConverter 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.StrokeThickness = 3;
pl.Points.Add(new Point(10, 10));
pl.Points.Add(new Point(50, 10));
pl.Points.Add(new Point(150, 150));
dragCanvas.Children.Add(pl);
return pl;
}

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

MultiBinding multiBinding = new MultiBinding();
multiBinding.Converter = new LineBindingConverter();
multiBinding.ConverterParameter = 1.0;

Binding binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("Left");
multiBinding.Bindings.Add(binding);

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("Width");
multiBinding.Bindings.Add(binding);

myLine.SetBinding(Line.X2Property, multiBinding);
pl.SetBinding(PolylineDPs.EndPointXProperty, multiBinding);

multiBinding = new MultiBinding();
multiBinding.Converter = new LineBindingConverter();
multiBinding.ConverterParameter = 0.3;

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("Top");
multiBinding.Bindings.Add(binding);

binding = new Binding();
binding.Source = myRect;
binding.Path = new PropertyPath("Height");
multiBinding.Bindings.Add(binding);

myLine.SetBinding(Line.Y2Property, multiBinding);
pl.SetBinding(PolylineDPs.EndPointYProperty, 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 BindingOperations.ClearAllBindings() or ClearBinding to clear
the binding(s):

#BindingOperations.ClearAllBindings Method (System.Windows.Data)
http://msdn2.microsoft.com/en-gb/lib...indingoperatio
ns.clearallbindings.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
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...
4
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...
3
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...
3
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...
4
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...
7
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: ...
23
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...
2
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...
0
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,...
0
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,...
0
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
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
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...
1
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...
0
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...
0
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
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
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 ...

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.