我能否在从数据模板获取行的WPF网格中获取表头?

5

我正在使用叠放的网格实现带标题的表格,以便可以指定列标题。有一个用于标题的网格,以及每行表格的一个网格。这种方式并不是很实用,因为必须两次指定标题宽度。也许我可以使用ListView/DataGrid而不进行所有样式设置?

我该如何摆脱这种多网格方法?

这是我目前的实现:

<StackPanel Orientation="Vertical">
  <Grid Margin="0, 10, 0, 0">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="40" />
      <ColumnDefinition Width="70" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Grid.Column="0" Grid.Row="0"
            Text="header 1" />
    <TextBlock Grid.Column="1" Grid.Row="0"
            Text="header 2" />
  </Grid>
  <ItemsControl ItemsSource="{Binding Entities}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Vertical">
          <Grid Margin="0, 10, 0, 0">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="40" />
              <ColumnDefinition Width="70" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0"
               Text="{Binding Property1}" />
            <TextBlock Grid.Column="1" Grid.Row="0"
               Text="{Binding Property2}" />
          </Grid>
        </StackPanel>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</StackPanel>
1个回答

10
你可以使用Grid.IsSharedSizeScope附加属性。
<StackPanel Orientation="Vertical" Grid.IsSharedSizeScope="True">
  <Grid Margin="0, 10, 0, 0">
    <Grid.ColumnDefinitions>
      <ColumnDefinition SharedSizeGroup="First" Width="40" />
      <ColumnDefinition SharedSizeGroup="Second" Width="70" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Grid.Column="0" Grid.Row="0"
            Text="header 1" />
    <TextBlock Grid.Column="1" Grid.Row="0"
            Text="header 2" />
  </Grid>
  <ItemsControl ItemsSource="{Binding Entities}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Vertical">
          <Grid Margin="0, 10, 0, 0">
            <Grid.ColumnDefinitions>
              <ColumnDefinition SharedSizeGroup="First" />
              <ColumnDefinition SharedSizeGroup="Second" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0"
               Text="{Binding Property1}" />
            <TextBlock Grid.Column="1" Grid.Row="0"
               Text="{Binding Property2}" />
          </Grid>
        </StackPanel>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</StackPanel>

没有起作用,我尝试了SharedSizeGroup="a"、SharedSizeGroup="b"等,但我只在第一组ColumnDefinitions中指定了宽度。 - Anders Lindén
也许使用多个网格也是一种技巧。 - Anders Lindén
@AndersLindén:我的错误:您必须将Grid.IsSharedSizeScope="True"作为StackPanel的属性。 - mathieu
我已经搞定了,看起来我需要处理多个网格,谢谢! - Anders Lindén
将宽度设置为“*”也是使宽度动态的一种方式,加上一些边距会更好... - Anders Lindén

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接