473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 9516
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.