创建一个WPF自定义控件 [英] Creating a WPF Custom Control

查看:189
本文介绍了创建一个WPF自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用WPF创建用户界面,并希望在我的窗口中显示一个饼图。据我所知,这将涉及创建自定义控件-这不是将样式或模板应用于现有控件就足够的方案。我已经阅读了几篇有关自定义控件的文章,所有提供详细说明的文章似乎都使用了非常不同的方法。

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.

不同的作者从中获得了他们的自定义控件类。其他,控件,用户控件或画布。使用以下几种不同方法之一在多个位置(包括OnRender和OnApplyTemplate)进行渲染:DrawingVisual,GeometryDrawing或Shape。似乎有很多不同的方法可以在屏幕上简单地放置一些形状,而现在我很困惑。

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.

是否存在公认的最佳方法? / p>

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

推荐答案

WPF工具包包括一个图表控件。由于源代码也可用,因此也许您可以看一下以获取启发。我还没有做很多调查,但是也许您想要的东西已经实现。

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.

对于WPF中的自定义控件,一般来说,我建议您获取 WPF控件开发已发布。我已经完成了一半,我必须说这是非常好的。它解释了有关创建自己的控件以及如何选择您应派生的类的所有内容。作者还解释了何时 not 创建自己的控件,并出色地完成了将最常见的WPF控件(如Slider或ListBox)变成漂亮的外观看起来像原始控件的可视控件。例如,他拿了一个ListBox并将其变成一个雷达屏幕,显示正确定位的船只和潜艇,甚至没有遗漏清扫锥。我怀疑您要完成的任务可以使用相同的技巧来完成。

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.

这篇关于创建一个WPF自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆