动态填充网格上的绑定问题 ActualWidth
请考虑以下简单代码:
XAML:
<Grid Height="60" Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="162*" />
<ColumnDefinition x:Name="coltest" Width="316*" />
<ColumnDefinition Width="239*" />
</Grid.ColumnDefinitions>
</Grid>
<Label MouseDoubleClick="TextBox_MouseDoubleClick"
Content="{Binding ElementName=coltest, Path=ActualWidth}" Grid.Row="1"/>
MouseDoubleClick
事件:
private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
grid.RowDefinitions.Add(new RowDefinition());
for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
{
Random r = new Random();
Label l = new Label { Content = r.Next(10, 1000000000).ToString() };
grid.Children.Add(l);
Grid.SetRow(l, grid.RowDefinitions.Count - 1);
Grid.SetColumn(l, i);
}
}
我的标签包含第二个标签的 ActualWidth
属性通过绑定的列。在 Visual Studio 中,我看到标签包含值 316,因此绑定有效。
双击标签会触发其事件并向网格添加额外的行,所有行的长度都是随机的。
我希望在我的标签上看到一个新值,但是(运行时计算的)0 不会改变!
我在这里缺少什么?
Consider the following, simple code:
XAML:
<Grid Height="60" Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="162*" />
<ColumnDefinition x:Name="coltest" Width="316*" />
<ColumnDefinition Width="239*" />
</Grid.ColumnDefinitions>
</Grid>
<Label MouseDoubleClick="TextBox_MouseDoubleClick"
Content="{Binding ElementName=coltest, Path=ActualWidth}" Grid.Row="1"/>
The MouseDoubleClick
event:
private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
grid.RowDefinitions.Add(new RowDefinition());
for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
{
Random r = new Random();
Label l = new Label { Content = r.Next(10, 1000000000).ToString() };
grid.Children.Add(l);
Grid.SetRow(l, grid.RowDefinitions.Count - 1);
Grid.SetColumn(l, i);
}
}
My label contains the ActualWidth
property of the second column via a binding. In Visual Studio I see my label containing the value 316, so the binding works.
Double clicking the label triggers its event and adds an extra row to the grid, all with a random length.
I expect to see a new value at my label, but (the at runtime calculated) 0 does not change!
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要问题是
ColumnDefinition
的ActualWidth
不是依赖属性,也没有实现INotifyPropertyChanged
,因此Binding 无法知道 coltest 的
ActualWidth
已更改。您需要显式更新
Binding
Edit2: 在这种情况下,您也许可以在
SizeChanged 中更新
事件,因为Binding
Grid
的Columns
具有*
宽度。不过,这对于Auto
宽度不会 100% 有效,因为宽度会根据ColumnDefinition
中的元素而变化事件处理程序
编辑:对 Xaml 进行了一些小更改。每次双击第一个
Label
事件处理程序时,这都会更新
Binding
The main problem is that
ActualWidth
of aColumnDefinition
isn't a Dependency Property, nor does it implementINotifyPropertyChanged
so theBinding
has no way of knowing that theActualWidth
of coltest has changed.You'll need to explicitly update the
Binding
Edit2: In this case, you might be able to update the
Binding
in theSizeChanged
event for theGrid
since theColumns
have*
width. This won't work 100% withAuto
width though since the width will change based on the elements in theColumnDefinition
Event handler
Edit: Made some small changes to the Xaml. This will update the
Binding
everytime you doubleclick the firstLabel
Event handler