绑定到自定义控件属性 [英] Binding to a custom control property

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

问题描述

你好,我有一个自定义控件.此自定义控件将其DataContext设置为一个实体.然后,在我的自定义控件中,我将一些TextBlocks绑定到该实体的各种属性.

Hello I have a custom control. This custom control has it's DataContext set to an entity. In my custom control I then bind some TextBlocks to various properties of this entity.

我还想将TextBlock绑定到控件类的属性.我不想/不需要通过XAML设置此属性.

I would also like to bind a TextBlock to a property of the control's class. I do not want/need to set this property through the XAML.

类似

<TextBlock Content="{Binding Path=MyControl.Property}" />

现在,我似乎正在尝试使用绑定的实体来解决此绑定,而不是我的自定义控件的类.

Right now, my it seems to be trying to use the bound entity to resolve this binding, rather than my custom control's class.

推荐答案

在您的自定义控件中,您应该使用要绑定的属性公开依赖项属性.

In your custom control you should expose a dependency property with the property you want to bind.

此外,您可能不想绑定文本块的内容,而是想绑定文本:

Also, you probably don't want to bind the content of the textblock, you want to bind the text:

<TextBlock Text="{Binding Path=MyControl.Property}" />

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

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