动态填充网格上的绑定问题 ActualWidth

发布于 2024-12-04 08:44:41 字数 1217 浏览 0 评论 0原文

请考虑以下简单代码:


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两个我 2024-12-11 08:44:41

主要问题是 ColumnDefinitionActualWidth 不是依赖属性,也没有实现 INotifyPropertyChanged,因此 Binding 无法知道 coltest 的 ActualWidth 已更改。

您需要显式更新 Binding

Edit2: 在这种情况下,您也许可以在 SizeChanged 中更新 Binding Grid 事件,因为 Columns 具有 * 宽度。不过,这对于 Auto 宽度不会 100% 有效,因为宽度会根据 ColumnDefinition 中的元素而变化

<Grid Name="grid"
      SizeChanged="grid_SizeChanged">
    <!--...-->
</Grid>

事件处理程序

void grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
    BindingExpression be = label.GetBindingExpression(Label.ContentProperty);
    be.UpdateTarget();
}

编辑:对 Xaml 进行了一些小更改。每次双击第一个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>

事件处理程序时,这都会更新Binding

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();
}

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.

You'll need to explicitly update the Binding

Edit2: 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>

Event handler

void grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
    BindingExpression be = label.GetBindingExpression(Label.ContentProperty);
    be.UpdateTarget();
}

Edit: 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>

Event handler

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文