[英]Set a BindingBase property as Binding in XAML
I have a BindingBase
property in the code-behind: 我在后面的代码中有一个BindingBase
属性:
public BindingBase MyBinding { get; set; }
In the code-behind, I can set the binding on a DependencyProperty
this way: 在后面的代码中,我可以通过以下方式在DependencyProperty
上设置绑定:
myTextBlock.SetBinding(TextBlock.TextProperty, MyBinding);
Is there any way to define it in XAML? 有什么方法可以在XAML中定义它吗?
I tried this, but it is not working: 我试过了,但是没有用:
<TextBlock Text="{Binding MyBinding}" />
as it tries to create a new binding to the MyBinding
property. 因为它尝试创建与MyBinding
属性的新绑定。
Your Source
and Target
should be of same type, or better use a Convertor
. 您的Source
和Target
应该是相同的类型,或者最好使用Convertor
。
TextBlock.Text
is string, and you are binding it to a Binding
type. TextBlock.Text
是字符串,并且您将其绑定到Binding
类型。 So, ToString()
would be called. 因此,将调用ToString()
。
And if you want to mimic what you have done in your code-behind, then you have to create a separate class eg public class CustomBinding : BindingBase
. 而且,如果您想模仿您在代码隐藏中所做的事情,则必须创建一个单独的类,例如, public class CustomBinding : BindingBase
。
And use it like this : 并像这样使用它:
<TextBlock>
<TextBlock.Text>
<local:CustomBinding>
...
</local:CustomBinding>
</TextBlock.Text>
</TextBlock>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.