WPF 网格中的独立宽度

Independent width in a WPF Grid(WPF 网格中的独立宽度)
本文介绍了WPF 网格中的独立宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 中有一个 2 行 2 列的网格.我希望每行的列宽都是独立的.我试过自动",但没有成功.下面是一张图来说明:

I have a grid with 2 rows and 2 columns in WPF. I would like that the column widths are independent for each row. I tried "auto", but no success. Here is a picture in order to explain:

如何使用网格来完成此操作?

How can I accomplish this using grid?

推荐答案

如果你必须使用网格布局,那么你有几个选择:

If you must use a grid layout, then you have a couple of options:

选项 1:将每一行设为单列,然后在您希望独立列的每一行中嵌套一个网格:

Option 1: Make each row a single column and then nest a grid in each row you would like independent columns:

XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <TextBlock Text="AAAAAAAAAAAAAAAAAAAA" />

    <Grid Grid.Row="1">
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>

      <TextBlock Text="BBBBBBB"">
      <TextBlock Grid.Column="1" Text="CCCCCCC" />
    </Grid>
</Grid>

选项 2:在行中使用 ColumnSpan:

Option 2: Make use of ColumnSpan in the rows:

XAML

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition />
  </Grid.RowDefinitions>

  <Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
  </Grid.ColumnDefinitions>

      <TextBlock Grid.ColumnSpan="2" Text="AAAAAAAAAAAAAAAAAAAA" />
      <TextBlock Grid.Row="1" Text="BBBBBBB"">
      <TextBlock Grid.Row="1" Grid.Column="1" Text="CCCCCCC" />
    </Grid>
</Grid>

*这些是在没有编辑器的情况下键入的,可能需要一些调整.

*These were typed without an editor and may need a bit of tweaking.

这篇关于WPF 网格中的独立宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)