views:

257

answers:

2

I'm currently creating a user interface using WPF and would like to display a pie-chart on my window. As far as I can see, this will involve creating a custom control - it is not a scenario in which applying styling or templating to an existing control will be sufficient. I've read several articles on custom controls and all of those which provide a detailed description seem to use very different methods.

Different authors derive their custom control classes from, among others, Control, UserControl or Canvas. Rendering is done in several places, including in OnRender and OnApplyTemplate, using one of several different methods: DrawingVisual, GeometryDrawing or Shape. There seem to be so many different ways of simply putting a few shapes on the screen that I am now very confused.

All I'm looking to achieve is a simple pie-chart, exposing an event which is raised whenever one of the wedges of the pie is clicked.

Is there a generally agreed "best" way to go about this?

+1  A: 

Checkout wpf toolkit on codeplex. They have chart controls on the roadmap.

Klerk
+2  A: 

The WPF Toolkit includes a Chart Control. Since the source code is also available, maybe you can take a look at it to get inspired. I haven't investigate it much but maybe what you seek is already implemented.

As for Custom Controls in WPF in general, I recommend you get a copy of WPF Control Development Unleashed. I'm about half way through it and I must say it is very good. It explains everything about creating your own controls and how to pick the class you should derive from. The author also explains when not to create your own control and does a great job of turning the most common WPF control like a Slider or a ListBox into a beautiful visual control that looks nothing like the original. For instance, he takes a ListBox and turns it into a radar screen showing boats and submarines correctly positioned, even the sweeping cone hasn't been omitted. I suspect that what you are trying to accomplish can be done using the same kind of trick.

Julien Poulin
Indeed, the toolkit includes a Chart, with a piechart implementation that you can assuredly retemplate to your needs if it doesn't quite fit. However, charting is only present in the preview release, if that is a concern for you.
Egor
Yup; it's nearly always possible to restyle/template a control into a different one -- but is it always the best way to do it? If you want a sweeping radar control with boats, it seems like using a style ListBox would be a lot more trouble than creating a custom control.
Robert Fraser