简体   繁体   English

如何公开XAML属性?

[英]How to expose xaml properties?

I created a ComboBox subclass and added my functionality. 我创建了一个ComboBox子类并添加了我的功能。

Now I want to expose external properties of the TextBox for example: 现在,我想公开TextBox的外部属性,例如:

<a:MyComboBox SpellCheck.IsEnabled="True" TextBox.SelectedText="{Binding X}" />

Is this possible, I maybe didn't choose the wrong particular property but I guess you understand what I mean. 这可能吗,我可能没有选择错误的特定属性,但我想您理解我的意思。

Is this possible? 这可能吗?
Do I have to create all the properties individually? 我必须单独创建所有属性吗?

This is not possible in XAML. 在XAML中这是不可能的。 XAML does not allow you to address sub-properties of individual members using a property path syntax like the one you describe. XAML不允许您使用所描述的属性路径语法来处理单个成员的子属性。 (Something similar is possible for certain inheritable properties such as backgrounds and font sizes, but that uses an inheritance mechanism -- affecting all contained controls -- rather than addressing a specific sub-element, and wouldn't work for TextBox.SelectedText anyway.) (类似的东西可能某些遗传特性,如背景和字体大小,但使用的继承机制-影响所有包含的控件-而非解决具体的子元素,并不会为TextBox.SelectedText工作反正。 )

So yes, you will need to declare custom properties on the MyComboBox class to surface the features of the TextBox that you need to access from outside the MyComboBox. 因此,是的,您将需要在MyComboBox类上声明自定义属性,以显示需要从MyComboBox外部访问的TextBox的功能。 On the plus side, this is good discipline for encapsulation: remember that a future developer might apply a custom template to your MyComboBox that doesn't include a TextBox (or the member isn't named TextBox, or the member named TextBox is actually a RichTextEditor instead of a plain TextBox...). 从好的方面来说,这是封装的良好纪律:请记住,将来的开发人员可能会将自定义模板应用于不包含TextBox的MyComboBox(或者该成员未命名为TextBox,或者该名为TextBox的成员实际上是一个RichTextEditor而不是普通的TextBox ...)。 Explicit properties ensure that MyComboBox defines clearly what behaviour and state such a template needs to respect, and lets the template decide how to implement that, rather than the author of the template being constrained always to expose a TextBox. 显式属性可确保MyComboBox清楚定义此类模板需要遵循的行为和状态,并让模板决定如何实现该行为和状态,而不是始终受限于模板的作者以公开TextBox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM