是否有 RowSpan="All"在 WPF 中?

发布于 2024-10-11 20:21:23 字数 422 浏览 9 评论 0 原文

我在网格中的 3 行中创建了一个 GridSplitter ,如下所示:

<GridSplitter Grid.Row="0" Grid.Column="1" Background="Yellow"
              HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
              Width="Auto" Height="Auto" ResizeDirection="Columns"
              Grid.RowSpan="3" ...

但是,可以想象我可能会在稍后阶段向网格中添加另一行,而且我真的不想这样做返回并更新我所有的行跨度。

我的第一个猜测是 Grid.RowSpan="*",但这无法编译。

I create a GridSplitter across the 3 rows that I have in my grid like this:

<GridSplitter Grid.Row="0" Grid.Column="1" Background="Yellow"
              HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
              Width="Auto" Height="Auto" ResizeDirection="Columns"
              Grid.RowSpan="3" ...

However, it's conceivable that I might add another row to my grid at a later stage, and I don't really want to go back and update all of my rowspans.

My first guess was Grid.RowSpan="*", but that doesn't compile.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

狂之美人 2024-10-18 20:21:24

一个简单的解决方案:

<!-- RowSpan == Int32.MaxValue -->
<GridSplitter Grid.Row="0"
              Grid.Column="1"
              Grid.RowSpan="2147483647" />

A simple solution:

<!-- RowSpan == Int32.MaxValue -->
<GridSplitter Grid.Row="0"
              Grid.Column="1"
              Grid.RowSpan="2147483647" />
蓝眼睛不忧郁 2024-10-18 20:21:24

您可以绑定到 RowDefinitions.Count,但在手动添加行时需要更新绑定。

编辑:实际上只是半手动
XAML:

<StackPanel Orientation="Vertical">
    <Grid Name="GridThing">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>   
            <ColumnDefinition/>         
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
            <RowDefinition />   
            </Grid.RowDefinitions>
            <Grid.Children>
                <Button Content="TopRight" Grid.Row="0" Grid.Column="1"/>
                <Button Content="LowerRight" Grid.Row="1" Grid.Column="1"/>
            <Button Content="Span Rows" Name="BSpan" Grid.RowSpan="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=RowDefinitions.Count, Mode=OneWay}"/>
        </Grid.Children>
        </Grid>
    <Button Click="Button_Click" Content="Add Row" />
</StackPanel>

代码:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        GridThing.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });
        foreach (FrameworkElement child in GridThing.Children)
        {
            BindingExpression exp = child.GetBindingExpression(Grid.RowSpanProperty);
            if (exp != null)
            {
                exp.UpdateTarget();
            }
        }
    }

You can bind to the RowDefinitions.Count but would need to update the binding when adding rows manually.

Edit: Only semi-manually in fact
Xaml:

<StackPanel Orientation="Vertical">
    <Grid Name="GridThing">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>   
            <ColumnDefinition/>         
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
            <RowDefinition />   
            </Grid.RowDefinitions>
            <Grid.Children>
                <Button Content="TopRight" Grid.Row="0" Grid.Column="1"/>
                <Button Content="LowerRight" Grid.Row="1" Grid.Column="1"/>
            <Button Content="Span Rows" Name="BSpan" Grid.RowSpan="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=RowDefinitions.Count, Mode=OneWay}"/>
        </Grid.Children>
        </Grid>
    <Button Click="Button_Click" Content="Add Row" />
</StackPanel>

Code:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        GridThing.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });
        foreach (FrameworkElement child in GridThing.Children)
        {
            BindingExpression exp = child.GetBindingExpression(Grid.RowSpanProperty);
            if (exp != null)
            {
                exp.UpdateTarget();
            }
        }
    }
新一帅帅 2024-10-18 20:21:24

Grid 控件没有提供任何开箱即用的功能。可以想象,您可以实现 MarkupExtension 或其他一些技巧来实现此目的。

The Grid control provides nothing like this out of the box. It's conceivable that you could implement a MarkupExtension or some other trickery to enable this.

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