当前位置: 动力学知识库 > 问答 > 编程问答 >

wpf - How to expose xaml properties?

问题描述:

I created a ComboBox subclass and added my functionality.

Now I want to expose external properties of the TextBox for example:

<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 does not allow you to address sub-properties of individual members using a property path syntax like the one you describe. (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.)

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. 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...). 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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: