绑定到 ColumnDefinition.ActualWidth 返回 0
将其粘贴到苹果酒中。
<Grid x:Name="Grid">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="ColumnDefinition"/>
</Grid.ColumnDefinitions>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding ActualWidth, ElementName=Grid}"/>
<TextBlock Text="{Binding ActualWidth, ElementName=ColumnDefinition}"/>
</StackPanel>
</Grid>
现在,运行它。第二个 TextBlock
显示 0,但不应该,对吧?解决方法?
Paste this into Cider.
<Grid x:Name="Grid">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="ColumnDefinition"/>
</Grid.ColumnDefinitions>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding ActualWidth, ElementName=Grid}"/>
<TextBlock Text="{Binding ActualWidth, ElementName=ColumnDefinition}"/>
</StackPanel>
</Grid>
Now, run it. The second TextBlock
shows 0, but shouldn't, right? Workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,你没有。实际上,ColumnDefinition 确实有一个 ActualWidth 属性。但它既不是 DependencyProperty 也不是 INotifyPropertyChanged 实现。因此,当
ActualWidth
更新时,没有人会让您的目标知道。解决方法?为了什么?您始终可以使用
ElementName
绑定到父FrameworkElement
,它具有您想要的ActualWidth
。抱歉,短语很复杂:)。不能让它变得更简单。希望这有帮助。
No, you didn't. ColumnDefinition does have an
ActualWidth
property, actually. But it's neitherDependencyProperty
nor thereINotifyPropertyChanged
implementation. So nobody let your target know whenActualWidth
is updated.Workaround? For what? You can always use
ElementName
binding to the parentFrameworkElement
, who has exactly thatActualWidth
that you wanted. Sorry for complex phrase :). Couldn't make it simpler.Hope this helps.
ColumnDefinition.ActualWidth
属性不是依赖属性,您的绑定只会显示它的初始值,在网格更新布局之前该值为 0。另一方面,Grid.ActualWidth 属性是一个依赖属性,绑定将随着宽度的变化而更新。The
ColumnDefinition.ActualWidth
property is not a dependency property and you binding will only show it's initial value which is 0 before the grid has been updated for layout. TheGrid.ActualWidth
property on the other hand is a dependency property and the binding will get updated as the width changes.来自 MSDN 页面:
不知何故,该值似乎在调用
Measure
之前就被绑定了。尝试在绑定上强制Mode = OneWay
,并手动检查代码,以确认此行为。From the MSDN page:
It would seem that the value is being bound before
Measure
is being called, somehow. Try forcingMode = OneWay
on your binding, and checking manually in code as well, to confirm this behaviour.