WPF TreeView“autowidth”几个项目?

时间:2016-09-18 09:20:25

标签: c# wpf treeview

在树视图中显示列的正确方法是什么,它们将宽度自动对齐到内容的宽度。 我看到的问题是,每个TreeViewItem是独立的,因此我不知道如何判断每个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>

1 个答案:

答案 0 :(得分:2)

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

请注意Grid.IsSharedSizeScope="True"中的TreeView属性和列定义中的SharedSizeGroup="A"