WPF树视图"autowidth"在几个项目上

本文关键字:quot 几个 项目 视图 autowidth WPF | 更新日期: 2023-09-27 18:07:21

在树视图中显示列的正确方法是什么,使它们的宽度自动对齐到内容的宽度。我看到的问题是,每个TreeViewItem是独立的,因此我不知道如何告诉eg日期列在每个TreeViewItem应具有相同的宽度,但取决于TreeViewItem与最宽的日期字符串?

  <TreeView Name="treeView" TreeViewItem.Expanded="TreeViewItem_Expanded" AutomationProperties.IsColumnHeader="True" AutomationProperties.IsRowHeader="True" AllowDrop="True" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
    <TreeViewItem>
      <TreeViewItem.Header>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
          </Grid.ColumnDefinitions>
          <Label>2.3.00</Label>
          <Label Grid.Column="2">Something</Label>
        </Grid>
      </TreeViewItem.Header>
    </TreeViewItem>
    <TreeViewItem>
      <TreeViewItem.Header>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
          </Grid.ColumnDefinitions>
          <Label>22.03.2000</Label>
          <Label Grid.Column="2">Something 2</Label>
        </Grid>
      </TreeViewItem.Header>
    </TreeViewItem>
  </TreeView>

WPF树视图"autowidth"在几个项目上

使用SharedSizeGroup

<TreeView Name="treeView" Grid.IsSharedSizeScope="True" TreeViewItem.Expanded="TreeViewItem_Expanded" AutomationProperties.IsColumnHeader="True" AutomationProperties.IsRowHeader="True" AllowDrop="True" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
  <TreeViewItem>
    <TreeViewItem.Header>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" SharedSizeGroup="A" />
          <ColumnDefinition Width="auto" SharedSizeGroup="B"  />
        </Grid.ColumnDefinitions>
        <Label>2.3.00</Label>
        <Label Grid.Column="2">Something</Label>
      </Grid>
    </TreeViewItem.Header>
  </TreeViewItem>
  <TreeViewItem>
    <TreeViewItem.Header>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" SharedSizeGroup="A"  />
          <ColumnDefinition Width="auto" SharedSizeGroup="B"  />
        </Grid.ColumnDefinitions>
        <Label>22.03.2000</Label>
        <Label Grid.Column="2">Something 2</Label>
      </Grid>
    </TreeViewItem.Header>
  </TreeViewItem>
</TreeView>

注意列定义中的TreeViewSharedSizeGroup="A"中的Grid.IsSharedSizeScope="True"属性