通过 DependencyProperty 字符串绑定到属性名称
我创建了一个带有 ItemContext
DependencyProperty
的 UserControl
。此属性包含控件的文本属性应绑定到的 DataContext
对象的属性名称。
我不知道如何在 XAML 中执行此操作。我尝试了几个步骤,距离很近,但找不到。
像这样的事情:
<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type me:UserControl}}, Path=ItemContext}" />
但是这里“ItemContext”的内容直接绑定到我不想要的文本属性。 “ItemContext”的内容假设“Property1”是我想要绑定到的 DataContext 中的属性的名称。
在代码中它的工作原理如下:
this.txtValue0.SetBinding(TextBox.TextProperty, new Binding(this.ItemContext) { Mode = BindingMode.TwoWay });
有人有想法吗?
谢谢
I've created a UserControl
with an ItemContext
DependencyProperty
. This property contains the name of the property of my DataContext
object the Control's text property should bind to.
I cannot figure out how to do this in XAML. I tried several steps, I'm quite near but couldn't find it.
Something like this:
<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type me:UserControl}}, Path=ItemContext}" />
But here the contents of "ItemContext" is bound directly to the Text Property which I don't want. The contents of "ItemContext" let's say "Property1" is the name of the property in my DataContext I'd like to bind to.
In code it works like this:
this.txtValue0.SetBinding(TextBox.TextProperty, new Binding(this.ItemContext) { Mode = BindingMode.TwoWay });
Does someone have an idea?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来好像您正在尝试将外部值传递到
Binding
对象的Path
属性中。也就是说,如果ItemContext
的值为“Blob”,您希望绑定到DataContext.Blob
(不显示值“Blob”)。这在代码中很容易做到,因为您可以直接引用该值(将
this.ItemContext
作为一次性值传递到绑定)。然而,在标记中,您不能这样做。相反,您尝试将一个值绑定到Binding
的Path
,但您不能(因为它不是 DependencyProperty) 。我建议一个更简单的解决方案是在 UserControl 上创建一个不同的属性:而不是传递“要绑定到的事物的名称”,为什么不只传递事物的值呢?
我想象您当前的代码如下所示:
... 相反,您应该使其看起来像这样:
... 因此该值在控件外部解析。在控件内,您可以使用@dowhilefor 的解决方案来绑定到该值。
希望这是有道理的!
It sounds as though what you're trying to pass an external value into the
Path
property of aBinding
object. That is, ifItemContext
's value is "Blob", you want to bind toDataContext.Blob
(not display the value "Blob").That's easy to do in code, because you can reference the value directly (you pass
this.ItemContext
to your binding as a one-time value). In markup, however, you can't do this. Instead, you're trying to bind a value to thePath
of theBinding
, but you can't (because it's not a DependencyProperty).I'd suggest that a much easier solution would be to create a different property on your UserControl: instead of passing in "the name of the thing you want to bind to," why not just pass in the value of the thing?
I'm imagining that your current code looks like this:
... and instead you should make it look like this:
... so the value is resolved externally of the control. Within the control, you can use @dowhilefor's solution to bind to the value.
Hope that makes sense!
如果我理解正确的话。您想在用户控件 xaml 中使用后面的用户控件代码中的依赖属性吗?
只需在 xaml 中为您的用户控件提供一个名称
x:Name="myUserControl"
并在您的 Binding 中写入{Binding ElementName=myUserControl, Path=MyDependencyProperty}。
至少就是这样我这样做,除非有人对这个奇怪的限制有更好的了解。If i understand it correctly. You want to use a dependency property from your usercontrol code behind, in your user control xaml?
Just give in xaml your user control a name
x:Name="myUserControl"
and in your Binding write{Binding ElementName=myUserControl, Path=MyDependencyProperty}.
Thats at least how i do it, unless someone has a better idea of that strange limitation.试试这个...
Xaml
代码隐藏
而不是与相对源绑定,我已为用户控件命名。我不知道相对源如何无法按预期使用自绑定......
Try this one...
Xaml
Code Behind
instead fo binding with relative source i have give name to user control. idon't some how relative source will not work as expected with self binding...