绑定到 UserControl 中的元素
假设我有一个如下所示的用户控件,我如何从控件外部将某些内容绑定到“G1”网格的 ActualWidth
?
<UserControl x:Class="Blah">
<WrapPanel>
<Grid x:Name="G1">
...
</Grid>
<Grid>
...
</Grid>
</WrapPanel>
</UserControl>
Say I have a user control like the one below, how would I bind something to the ActualWidth
of the "G1" grid from outside of the control?
<UserControl x:Class="Blah">
<WrapPanel>
<Grid x:Name="G1">
...
</Grid>
<Grid>
...
</Grid>
</WrapPanel>
</UserControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的意思是在控件外部,而不是作为控件的内容,则可以在 Binding 中使用
ElementName
,如下所示:如果您的意思是在另一个 Xaml 文件中的控件外部,那么您可以尝试使用如果您的控件在另一个控件的范围内,则路径属性:
但是我建议不要这种设计,因为您可能有一天会更改 G1 的名称,并且您永远不会知道任何可能会破坏的绑定。
If you mean with outside the control, not as Content of the control, you can use
ElementName
in the Binding like so:If you mean outside the control in another Xaml file, then you can try to use the Path property if your control is in the scope of the other control:
However I would advise against this design, because you may change the name of G1 one day, and you would never know of any bindings that might break.
如果要绑定到使用此用户控件的外部控件,请在 UserControl 代码后面声明一个
DependencyProperty
,然后将 G1 绑定到该属性。并将外部控件的属性绑定到 UserControl 的
DependencyProperty
。它就像一个 2 级间接。
If you want to bind to an external control where you use this user control, declare a
DependencyProperty
at your UserControl code behind and then Bind G1 to that property.And bind the external control's property to the UserControl's
DependencyProperty
.It is like a 2 level of indirection.