动态网格填充绑定问题ActualWidth的 [英] Binding issue ActualWidth on dynamic filled Grid
问题描述
考虑下面,简单code:
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"/>
的 MouseDoubleClick
事件:
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);
}
}
我的标签包含通过绑定第二列的 ActualWidth的
属性。在Visual Studio中我看到包含我的标签值316,所以结合的作品。
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.
我希望看到我的标签的新价值,但(该计算运行时间)0不会改变!
I expect to see a new value at my label, but (the at runtime calculated) 0 does not change!
我是什么在这里失踪?
推荐答案
主要的问题是, ActualWidth的
的 ColumnDefinition
不是一个依赖项属性,也没有实施 INotifyPropertyChanged的
所以绑定
没有办法知道的 ActualWidth的
COLTEST已经改变了。
The main problem is that ActualWidth
of a ColumnDefinition
isn't a Dependency Property, nor does it implement INotifyPropertyChanged
so the Binding
has no way of knowing that the ActualWidth
of coltest has changed.
您需要显式地更新绑定
EDIT2:在这种情况下,你也许可以在时,SizeChanged $ C更新
绑定
$ C>事件的电网
因为列
有 *
宽度。这不会与自动
工作宽度100%,但由于宽度将改变基于 ColumnDefinition
<元素/ p>
In this case, you might be able to update the Binding
in the SizeChanged
event for the Grid
since the Columns
have *
width. This won't work 100% with Auto
width though since the width will change based on the elements in the ColumnDefinition
<Grid Name="grid"
SizeChanged="grid_SizeChanged">
<!--...-->
</Grid>
的事件处理程序的
void grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
BindingExpression be = label.GetBindingExpression(Label.ContentProperty);
be.UpdateTarget();
}
编辑:提出了一些小的变化到XAML。这将更新您双击第一个标签上的
绑定
每次
Made some small changes to the Xaml. This will update the Binding
everytime you doubleclick the first Label
<Grid Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="162*" />
<ColumnDefinition x:Name="coltest" Width="316*" />
<ColumnDefinition Width="239*" />
<ColumnDefinition Width="239*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label MouseDoubleClick="TextBox_MouseDoubleClick"
Name="label"
Content="{Binding ElementName=coltest, Path=ActualWidth}" Grid.Row="0"/>
</Grid>
的事件处理程序的
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);
}
BindingExpression be = label.GetBindingExpression(Label.ContentProperty);
be.UpdateTarget();
}
这篇关于动态网格填充绑定问题ActualWidth的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!