构建WPF自定义控件 [英] Building WPF Custom Controls

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

问题描述

在那里

我正在构建一个c#wpf多点触摸应用程序,该应用程序由屏幕上的多个对象组成,这些对象可以使用触摸进行操作.这些信息对象"将显示各种类型的信息,例如文本,网页,图像,视频.

目前,我已经用代码定义了对象,扩展了Canvas类并以编程方式添加了所有可视元素.信息对象有几种类型,一种用于图像,一种用于网页等,但是它们都从基类继承以实现通用功能.

我希望能够使用xaml定义对象的外观,并且希望能够在其他应用程序中重用它们.因此,我正在考虑将对象类重写为自定义控件的库.

我已尝试按照演练说明如何执行此看似简单的任务,但到目前为止,我遇到了障碍.如果有人可以帮助我制作自定义控件,我将不胜感激?

1.我创建一个名为"WPF自定义控件库"的新项目,称为InformationControlLibrary
2.由于Generic.xaml给出错误,我重新构建项目
3.我将CustomControl1类重命名为ImageInformationControl
4.我创建"WPF应用程序"类型的新测试项目
5.我在测试应用程序中创建对自定义控件库的引用.

我现在可以在工具箱中看到我的第一个控件,但是由于我仍然需要对其进行定义,因此我无法使用它.我收集到我需要扩展一些视觉元素(可能是UIElement吗?),但是我不能通过扩展Control来更改CustomControl1类.我在哪里可以做到这一点,以及如何编辑XAML?所有控件都在generic.xaml中完成了吗?

希望您能帮忙,谢谢您的时间:)

hi there

i''m building a c# wpf multi-touch application which consists of multiple objects on the screen which can be manipulated using touch. these "information objects" will display various types of information such as text, webpages, images, video.

presently i have my objects defined in code, extending the Canvas class and programmatically adding all the visual elements. there are several types of information object, one for images, one for webpages, etc. but which all inherit from a base class for common functionality.

i would like to be able to use xaml to define the look of my objects, and i would like to be able to reuse them in other applications. i am thus considering rewriting my object classes as a library of custom controls.

i have tried following walkthroughs for how to do this seemingly simple task, but so far i have run into roadblocks. i would really appreciate if anyone could help me through producing a custom control?

1. i create a new project of type "WPF Custom Control Library" called InformationControlLibrary
2. i rebuild the project since Generic.xaml is giving an error
3. i rename the CustomControl1 class to ImageInformationControl
4. i create a new test project of type "WPF Application"
5. i create a reference to the custom control library in my test application.

i can now see my first control in the toolbox, but i can''t use it yet as i still need to define it. i gather that i need to extend some visual element (probably UIElement?) but i can''t change the CustomControl1 class from extending Control. where can i do this, and how can i edit the xaml? is it all done in generic.xaml for all of the controls?

i hope you can help, thanks for your time :)

推荐答案

您写道:
我收集到我需要扩展一些视觉元素(可能是UIElement吗?),但是我不能通过扩展Control来更改CustomControl1类"

控件继承自FrameworkElement,而FrameworkElement继承自UIElement :)

据我了解,您想创建可重用的CustomControl,其中将包含其他对象的常用功能?从ContentControl而不是Control继承CustomCotrol怎么办?

例如,在我的一个项目中,我需要在画布上创建许多可调整大小的控件.我创建了CustomControl,它扩展了ContentControl并实现了调整大小功能.

之后,我只是将所需的控件放入自定义可调整大小控件的Content中,例如:

you wrote:
"i gather that i need to extend some visual element (probably UIElement?) but i cant change the CustomControl1 class from extending Control"

Control is inherited from FrameworkElement which is inherited from UIElement :)

As I understand you want to create reusable CustomControl which will contain common functionality for other objects? What about to inherit your CustomCotrol from ContentControl instead of Control?

For example, in one of my projects I need create a lot of resizable controls on canvas. I created CustomControl which extends ContentControl and which implements resize feature.

After that I just put needed controls into Content of my custom resizable control, like:

<controls:resizablecontrol xmlns:controls="#unknown">
   <controls:foldernode title="Parent" />
</controls:resizablecontrol>


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

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