更改WPF网格以使原点位于左下角

时间:2014-01-07 18:01:27

标签: c# .net wpf xaml

如果我有这样的控件:

<Grid ShowGridLines="True">  
  <Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition/>
    <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
    <RowDefinition/>
  </Grid.RowDefinitions>

  <TextBlock Grid.Column="0" Grid.Row="0" Text="One, One" />
  <TextBlock Grid.Column="0" Grid.Row="1" Text="One, Two" />
  <TextBlock Grid.Column="0" Grid.Row="2" Text="One, Three" />
  <TextBlock Grid.Column="1" Grid.Row="0" Text="Two, One" />
  <TextBlock Grid.Column="1" Grid.Row="1" Text="Two, Two" />
  <TextBlock Grid.Column="1" Grid.Row="2" Text="Two, Three" />
  <TextBlock Grid.Column="2" Grid.Row="0" Text="Three, One" />
  <TextBlock Grid.Column="2" Grid.Row="1" Text="Three, Two" />
  <TextBlock Grid.Column="2" Grid.Row="2" Text="Three, Three" />
</Grid>

我会得到一个看起来像这样的网格:

___________________________________________  
| One, One   | Two, One   |  Three, One   |
-------------------------------------------
| One, Two   | Two, Two   |  Three, Two   |
-------------------------------------------
| One, Three | Two, Three |  Three, Three |
-------------------------------------------

我有什么可以做的(除了更改我的所有Grid.ColumnGrid.Row设置)以使此网格显示如下:

___________________________________________  
| One, Three | Two, Three |  Three, Three |
-------------------------------------------
| One, Two   | Two, Two   |  Three, Two   |
-------------------------------------------
| One, One   | Two, One   |  Three, One   |
-------------------------------------------

基本上我想将原点从左上角改为左下角。

(注意:我的真实场景有一个42 x 30网格,其中有许多可视组件绑定到网格(按行,列和跨度)。这个问题是我试图避免手动转换绑定数据。)

1 个答案:

答案 0 :(得分:5)

您可以使用布局转换执行此操作。 编辑:将转换简化为单个转换。事实证明,网格的矩阵变换与每个文本块的矩阵变换相匹配

以下是一个示例屏幕截图:

Example

<Grid ShowGridLines="True">
    <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <MatrixTransform>
                        <MatrixTransform.Matrix>
                            <Matrix M11="1" M12="0" M21="0" M22="-1"  />
                        </MatrixTransform.Matrix>
                    </MatrixTransform>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <Grid.LayoutTransform>
        <MatrixTransform>
            <MatrixTransform.Matrix>
                <Matrix M11="1" M12="0" M21="0" M22="-1"  />
            </MatrixTransform.Matrix>
        </MatrixTransform>
    </Grid.LayoutTransform>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Column="0" Grid.Row="0" Text="One, One"/>
    <TextBlock Grid.Column="0" Grid.Row="1" Text="One, Two"/>
    <TextBlock Grid.Column="0" Grid.Row="2" Text="One, Three"/>
    <TextBlock Grid.Column="1" Grid.Row="0" Text="Two, One" />
    <TextBlock Grid.Column="1" Grid.Row="1" Text="Two, Two" />
    <TextBlock Grid.Column="1" Grid.Row="2" Text="Two, Three" />
    <TextBlock Grid.Column="2" Grid.Row="0" Text="Three, One" />
    <TextBlock Grid.Column="2" Grid.Row="1" Text="Three, Two" />
    <TextBlock Grid.Column="2" Grid.Row="2" Text="Three, Three" />
</Grid>