WPF中是否有RowSpan ="All"?

Chr*_*cer 29 wpf grid-layout

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

<GridSplitter Grid.Row="0" Grid.Column="1" Background="Yellow"
              HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
              Width="Auto" Height="Auto" ResizeDirection="Columns"
              Grid.RowSpan="3" ...
Run Code Online (Sandbox Code Playgroud)

但是,可以想象我可能会在以后的阶段向我的网格中添加另一行,我真的不想回去更新所有的rowpans.

我的第一个猜测是Grid.RowSpan="*",但这不会编译.

use*_*116 34

简单的解决方案:

<!-- RowSpan == Int32.MaxValue -->
<GridSplitter Grid.Row="0"
              Grid.Column="1"
              Grid.RowSpan="2147483647" />
Run Code Online (Sandbox Code Playgroud)

  • 你这么简单吗?如果您要引用System,您可以直接使用{x:Static sys:Int32.MaxValue}. (12认同)
  • 保持简单:)虽然您可能想使用类似`<sys:Int32 x:Key ="SpanAll"> 2147483647 </ sys:Int32>`和`Grid.RowSpan ="{StaticResource SpanAll}"`来制作其他内容人们明白发生了什么:) (8认同)
  • 呵呵是的,这个数字在经过多年的开发后很容易识别,但它看起来很奇怪,就像RowSpan的值一样,就像有人在键盘上掉了东西:) (6认同)
  • 另外,这是因为`Column.Min`在`ColumnSpanProperty`和`RowSpanProperty`上调用了剩余列数或行数. (2认同)
  • SpanAll 不适合设计器,2147... 太长了,我只需要 100,如果我在我的网格中创建更多 100 列或行,我无论如何都做错了。 (2认同)

H.B*_*.B. 17

您可以绑定到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>
Run Code Online (Sandbox Code Playgroud)

码:

    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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 确保Grid.RowSpan绑定是Mode = OneTime,否则会出现内存泄漏.RowDefinitions不是DependencyProperty,因此如果绑定不是OneTime,则会发生内存泄漏.https://support.microsoft.com/en-us/kb/938416 (2认同)
  • @GLewis:你有没有读过答案?所有这些都被考虑在内. (2认同)