473,387 Members | 1,834 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,387 software developers and data experts.

RE: Need help with a custom shape class for polylines in wpf

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));
_points.Add(new Point(10, 100));
_points.Add(new Point(100, 100));

Because while the application is running, the override DefiningGeometry
method will be called for several times which in turn calls the GetGeometry
method, there will be more than 3 points added to the _points collection.

You can move the above lines of code to the constructor of the
CustomPolyLine class.

2. To get a custom shape to refresh in UI automatically when the value of
properties of the custom shape are changed at run time, you need to back
the CLR properties with dependency properties. For example:

public PointCollection Points
{
get
{
return (PointCollection )base.GetValue(PointsProperty);
}
set
{
base.SetValue(PointsProperty,value);
}
}

public static readonly DependencyProperty PointsProperty =
DependencyProperty.Register("PointsProperty",
typeof(PointCollection), typeof(CustomPolyLine), new
FrameworkPropertyMetadata(new PointCollection() { new Point(10, 10), new
Point(10, 100), new Point(100, 100) },
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsMeasure));

NOTE: Only when the value of a dependency property is changed, the instance
will be refreshed in UI. As for the Points property in the above sample
code, the PointCollection is a reference type. Only when the Points
property is set to a new instance of type PointCollection, the custom shape
will refresh in UI.

In other words, if you add a Point object to the Points property, the
custom shape won't refresh in UI because the value of the Points property
is not changed(it still refers to the same object as before).

3. To workaround the problem, we can use ObservableCollection<Ttype
instead of the PointCollection type because the ObservableCollection<T>
provides list changes notification. Then subscribe the CollectionChanged
event to refresh the custom shape in UI manually. The following is the
complete modified code of the CustomPolyLine class.

public class CustomPolyLine : Shape
{
public CustomPolyLine()
{
// subscribe the CollectionChanged event of the Points property
Points.CollectionChanged += Points_CollectionChanged;
}

void Points_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionCha ngedEventArgs e)
{
// invalidate the custom shape manually
base.InvalidateMeasure();
}
// use the type of ObservableCollection<Pointinstead of
PointCollection, because ObservableCollection<Pointprovides list changes
notification
public ObservableCollection<PointPoints
{
get
{
return
(ObservableCollection<Point>)base.GetValue(PointsP roperty);
}
set
{
base.SetValue(PointsProperty,value);
}
}

public static readonly DependencyProperty PointsProperty =
DependencyProperty.Register("PointsProperty",
typeof(ObservableCollection<Point>), typeof(CustomPolyLine), new
FrameworkPropertyMetadata(new ObservableCollection<Point>() { new Point(10,
10), new Point(10, 100), new Point(100, 100) },
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsMeasure));

protected override Geometry DefiningGeometry
{
get
{
// Create a StreamGeometry for describing the shape
StreamGeometry geometry = new StreamGeometry();
geometry.FillRule = FillRule.EvenOdd;

using (StreamGeometryContext context = geometry.Open())
{
GetGeometry(context);
}

// Freeze the geometry for performance benefits
geometry.Freeze();
return geometry;

}
}

void GetGeometry(StreamGeometryContext context)
{
context.BeginFigure(Points[0], true, false);

foreach (Point pt in Points)
{
context.LineTo(pt, true, true);
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

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.
Jun 27 '08 #1
4 3873
Linda, Thanks for the explanations! this really helps me understand what's
going on. I wont be able to try it for a few days, but will let you know if
I have any further questions.
"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:bX**************@TK2MSFTNGHUB02.phx.gbl...
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));
_points.Add(new Point(10, 100));
_points.Add(new Point(100, 100));

Because while the application is running, the override DefiningGeometry
method will be called for several times which in turn calls the
GetGeometry
method, there will be more than 3 points added to the _points collection.

You can move the above lines of code to the constructor of the
CustomPolyLine class.

2. To get a custom shape to refresh in UI automatically when the value of
properties of the custom shape are changed at run time, you need to back
the CLR properties with dependency properties. For example:

public PointCollection Points
{
get
{
return (PointCollection )base.GetValue(PointsProperty);
}
set
{
base.SetValue(PointsProperty,value);
}
}

public static readonly DependencyProperty PointsProperty =
DependencyProperty.Register("PointsProperty",
typeof(PointCollection), typeof(CustomPolyLine), new
FrameworkPropertyMetadata(new PointCollection() { new Point(10, 10), new
Point(10, 100), new Point(100, 100) },
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsMeasure));

NOTE: Only when the value of a dependency property is changed, the
instance
will be refreshed in UI. As for the Points property in the above sample
code, the PointCollection is a reference type. Only when the Points
property is set to a new instance of type PointCollection, the custom
shape
will refresh in UI.

In other words, if you add a Point object to the Points property, the
custom shape won't refresh in UI because the value of the Points property
is not changed(it still refers to the same object as before).

3. To workaround the problem, we can use ObservableCollection<Ttype
instead of the PointCollection type because the ObservableCollection<T>
provides list changes notification. Then subscribe the CollectionChanged
event to refresh the custom shape in UI manually. The following is the
complete modified code of the CustomPolyLine class.

public class CustomPolyLine : Shape
{
public CustomPolyLine()
{
// subscribe the CollectionChanged event of the Points property
Points.CollectionChanged += Points_CollectionChanged;
}

void Points_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionCha ngedEventArgs e)
{
// invalidate the custom shape manually
base.InvalidateMeasure();
}
// use the type of ObservableCollection<Pointinstead of
PointCollection, because ObservableCollection<Pointprovides list changes
notification
public ObservableCollection<PointPoints
{
get
{
return
(ObservableCollection<Point>)base.GetValue(PointsP roperty);
}
set
{
base.SetValue(PointsProperty,value);
}
}

public static readonly DependencyProperty PointsProperty =
DependencyProperty.Register("PointsProperty",
typeof(ObservableCollection<Point>), typeof(CustomPolyLine), new
FrameworkPropertyMetadata(new ObservableCollection<Point>() { new
Point(10,
10), new Point(10, 100), new Point(100, 100) },
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsMeasure));

protected override Geometry DefiningGeometry
{
get
{
// Create a StreamGeometry for describing the shape
StreamGeometry geometry = new StreamGeometry();
geometry.FillRule = FillRule.EvenOdd;

using (StreamGeometryContext context = geometry.Open())
{
GetGeometry(context);
}

// Freeze the geometry for performance benefits
geometry.Freeze();
return geometry;

}
}

void GetGeometry(StreamGeometryContext context)
{
context.BeginFigure(Points[0], true, false);

foreach (Point pt in Points)
{
context.LineTo(pt, true, true);
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

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.


Jun 27 '08 #2
Hi Moondaddy,

I am reviewing this post in the newsgroup and would like to know the status
of this issue.

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #3
Hi Linda,

I followed your recommendation and it works fine now.

"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:1S**************@TK2MSFTNGHUB02.phx.gbl...
Hi Moondaddy,

I am reviewing this post in the newsgroup and would like to know the
status
of this issue.

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

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


Jun 27 '08 #4
Hi Moondaddy,

Thank you for your confirmation! I am glad to hear that the problem is
solved now.

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Have a good day!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #5

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

Similar topics

1
by: E. Robert Tisdale | last post by:
Please find attached a copy of the "Shape class" example from Bjarne Stroustrup, "The C++ Programming Language: Third Edition", Chapter 2: A Tour of C++, Section 6: Object-Oriented Programming,...
0
by: Shailaja Kulkarni | last post by:
Hi All, I am new to component development. I want to create custom control to arrange contained controls in form of polygonal shape. The objects are placed in separate panel on the some form....
1
by: orel | last post by:
Please, As i tried hundreds different implementation to make it work and actually didn't succeed, can someone here help me to understand and use the implementation of the Object Factory design...
4
by: orel | last post by:
<o_r_l_25@yahoo.frwrote, As the message says, to help the compiler with template parsing, I add 'typename' as you said, it now understands the expression, but but i have now those complier...
3
by: Dean Craig | last post by:
I'm working with the new ASP.NET AJAX Control Toolkit. I have a map that has several key areas (hot spots) where when the user hovers over them, I want to pop up a small window with information in...
9
by: Chrissy | last post by:
I took a C# class as an elective and received an incomplete in it and am desparate for help. I have two assignments left (arrays and inheritance) and would gladly pay anyone that can assist me with...
3
by: hjast | last post by:
test has exited due to signal 10 (SIGBUS). This is the circle class #include <iostream.h> class Shape {
2
by: Angus | last post by:
Hello I want to use polymorphic feature. Eg if I had a base Shape class and a derived Circle class I could do things like this: Shape myShape; MyCircle* pCircle = &myShape;...
32
by: falconsx23 | last post by:
I am making a game called Set, it is a card game: here is a brief description of the rules: The game of Set is played with 81 cards, each of which has 4 properties (number, pattern, color and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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,...

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.