使用Width =“Auto”添加额外的填充

时间:2016-06-29 08:52:09

标签: wpf listview

我有一个包含多个GridViewColumns的Listview,我为每个列使用Width =“Auto”,这确保了列的大小根据它们的最大元素。

我想为每个列标题添加额外的呼吸空间,类似于以下内容: 宽度=“自动”+ 30

我已尝试设置边距和填充但是这会导致在调整大小时过早地切断列文本标题。 关于如何实现这一目标的任何想法?

我的GridViewColumn目前?

<GridViewColumn Width="Auto">
        <GridViewColumn.Header>
            <GridViewColumnHeader HorizontalContentAlignment="Left"
                                  Content="{x:Static resources:Resources.TableHeadingDescription}" 
                                  SizeChanged="Description_Column_SizeChanged">
            </GridViewColumnHeader>
        </GridViewColumn.Header>
        <GridViewColumn.CellTemplate>
            <DataTemplate>
            <TextBlock Name="DescriptionTextBlock" 
                       Text="{Binding Description}" 
                       Style="{StaticResource TextBlock_GridEntries_Style}"
                       Margin="{Binding HierarchyLevel, Mode=OneWay, Converter={StaticResource HierarchyToMarginConverter}}"/>
            </DataTemplate>
        </GridViewColumn.CellTemplate>
 </GridViewColumn>

1 个答案:

答案 0 :(得分:0)

将ContentTemplate添加到GridViewColumnHeader,并在模板中设置元素上的一些边距

<GridViewColumn Width="Auto">
    <GridViewColumn.Header>
        <GridViewColumnHeader HorizontalContentAlignment="Left"
                              Content="{x:Static resources:Resources.TableHeadingDescription}" 
                              SizeChanged="Description_Column_SizeChanged">
            <GridViewColumnHeader.ContentTemplate>
                <DataTemplate>
                    <Label Margin="30,0" Content="{Binding}" />
                </DataTemplate>
            </GridViewColumnHeader.ContentTemplate>
        </GridViewColumnHeader>
    </GridViewColumn.Header>
    <GridViewColumn.CellTemplate>
        <DataTemplate>
        <TextBlock Name="DescriptionTextBlock" 
                   Text="{Binding Description}" 
                   Style="{StaticResource TextBlock_GridEntries_Style}"
                   Margin="{Binding HierarchyLevel, Mode=OneWay, Converter={StaticResource HierarchyToMarginConverter}}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
 </GridViewColumn>