【问题标题】:Set multiple TextBlock inside a GridViewColumn在 GridViewColumn 内设置多个 TextBlock
【发布时间】:2016-08-31 18:34:42
【问题描述】:

我正在尝试在 GridViewColumn 中设置两个 TextBlock

<GridViewColumn.CellTemplate>
  <DataTemplate>
    <TextBlock TextWrapping="Wrap" Text="{Binding PlayerA}" />
    <TextBlock TextWrapping="Wrap" />
  </DataTemplate>
</GridViewColumn.CellTemplate>

但我知道内容已经设置好了,为什么?

【问题讨论】:

    标签: wpf gridviewcolumn


    【解决方案1】:

    DataTemplate 属性只能有一个孩子。你正在设置两个孩子,两个TextBoxes

    您必须将TextBoxes 包含在一个通用容器中。

    如果你想要一个简单的水平连接,你可以这样写:

    <GridViewColumn.CellTemplate>
      <DataTemplate>
        <StackPanel Orientation="Horizontal">
          <TextBlock Text="blabla1"/>
          <TextBlock Text="blabla2"/>
        </StackPanel>
      </DataTemplate>
    </GridViewColumn.CellTemplate>
    

    如果您希望Width 在两个TextBoxes 之间平均分配,您可以这样写:

    <GridViewColumn.CellTemplate>
      <DataTemplate>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>
          <TextBlock Grid.Column="0" Text="1"/>
          <TextBlock Grid.Column="1" Text="2"/>
        </Grid>
      </DataTemplate>
    </GridViewColumn.CellTemplate>
    

    WPF 容器有很多,这取决于你想要实现什么布局,但规则是:DataTemplate 必须只包含一个元素

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-20
      • 2013-08-23
      • 1970-01-01
      • 2014-11-18
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      相关资源
      最近更新 更多