c# - 我可以在 WPF 网格中获取标题,从数据模板中获取其行吗?

标签 c# wpf xaml

我正在使用彼此重叠的网格实现带标题的表格,因此我可以指定列标题。标题有一个网格,表格中的每一行都有一个网格。这不是很实用,标题宽度必须指定两次。也许我可以拥有一个没有所有样式的 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>

最佳答案

您可以使用 Grid.IsSharedSizeScope附属属性(property)

<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>

关于c# - 我可以在 WPF 网格中获取标题,从数据模板中获取其行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15611336/

相关文章:

c# - 配置文件中的节类型

c# - 将德语字符(变音符号、重音符号)替换为英语对应字符

c# - WPF 命令 - 从 Window 和 UserControl 调用,相同的处理程序

c# - 类库中的可移植 XAML 样式

c# - 如何控制作为绑定(bind)源的TextBox文本何时更新目标值?

c# - 一个类 ViewModel 中的 AutoMapper 多个类,每个类都有属性

c# - Response.Redirect() 禁用后退按钮

c# - 如何使 CM View 显示两个 subview 之一,所有(包括父)都绑定(bind)到同一个 View 模型?

c# - .NET 本地化失败

c# - 使用 C# 代码时查看控件列表?