为用户控件实现自定义内容属性 [英] Implement a custom content property for user controls

查看:64
本文介绍了为用户控件实现自定义内容属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



基本上我有一个类似按钮的自定义用户控件。使用按钮,您可以设置内容以在按钮中放置一个字符串:

Hello,

Basically I have a custom user control similar to a button. With a button you can set the Content to place a string within your button:

<Button Content="Hello World"/>



我想做类似的事情,让用户控制一个标签,让用户控件的content属性改变标签'。文本。

到目前为止,我无法上班,我尝试了几件事。我真的不太确定要谷歌了。



任何人都应该知道我应该看看什么?

或者是一些示例代码?


I want to do something like that, have a user control with among things a label and let the content property of the user control change the label''s text.
I can''t get it to work so far, I''ve tried a couple of things. I''m not really even sure what to google anymore.

Anyone an idea where I should look?
Or maybe some example code?

推荐答案

这里你需要知道的是WPF 内容模型

http://msdn.microsoft.com/en-us/library/bb613548.aspx [ ^ ]。



如果你看一下,你会看到,例如, System.Windows.Controls.Button 派生自类 System.Windows.Controls.ContentControl ,因此,它的内容是一个任意对象:

http://msdn.microsoft.com/en-us/library/system.windows.controls.button.aspx [ ^ ]。



您的类派生自 System.Windows.Controls.UserControl ,因此,也来自类 System.Windows.Controls.ContentControl 。请参阅:

http:// msdn .microsoft.com / zh-cn / library / system.windows.controls.contentcontrol.aspx# inheritanceContinued [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol.aspx [< a href =http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol.aspxtarget =_ blanktitle =New Window> ^ ]。



这意味着,基本上,它在内容模型中被视为按钮;它的内容已经是一个任意对象。还需要什么习惯?您的自定义控件类还继承 Content 属性。你不需要让它更自定义,因为它已经足够自定义:

http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content.aspx [ ^ ]。br />


但是,实际上,为了让你内容正确使用WPF UI,你不应该完全使用内容随心所欲。在大多数情况下,用户控件的开发人员使用某些类 Panel 作为内容,更准确地说,是从 Panel ;并且该面板包含一些其他UI元素:

http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx#inheritanceContinued [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.uielement.aspx [ ^ ]。



有什么问题吗?



-SA
What you need to know here is the WPF content model:
http://msdn.microsoft.com/en-us/library/bb613548.aspx[^].

If you look at it, you will see that, for example, System.Windows.Controls.Button is derived from the class System.Windows.Controls.ContentControl, and, hence, it''s content is "a single arbitrary object":
http://msdn.microsoft.com/en-us/library/system.windows.controls.button.aspx[^].

Your class derived from System.Windows.Controls.UserControl and, hence, also from the class System.Windows.Controls.ContentControl. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx#inheritanceContinued[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol.aspx[^].

It means that, basically, it is considered in the content model pretty much as the button; it''s content is already "a single arbitrary object". What more "custom" would you need? And your custom control class also inherits the Content property. You don''t need to make it more "custom" because it is already "custom enough":
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content.aspx[^].

However, in practice, to make you Content working with the WPF UI properly, you should not make the content totally arbitrary. In most cases, developers of the user controls use some class of Panel as the content, more exactly, one of the classes derived from Panel; and the panel holds some other UI Elements:
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx#inheritanceContinued[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.aspx[^].

Any problems?

—SA

这篇关于为用户控件实现自定义内容属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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