简体   繁体   中英

GridViewColumn Header content does not occupy all the space

I have a GridViewColumn the content of which is a TextBlock:

GridViewColumn column = new GridViewColumn();
column.Header = new TextBlock { Text = header };
MyGridView.Columns.Add(column);

I change run-time the TextBlock background (black) of the second column, and this is the result:

在此处输入图片说明

The TextBlock does not occupy all available space.
Why? How can I fix this?
Thanks.

A TextBlock is only as big as its contents by default. Try this:

column.Header = new TextBlock { Text = header, HorizontalAlignment = HorizontalAlignment.Stretch };

Doco here: System.Windows.HorizontalAlignment .

Edit:

Your problem is because you are doing things inside a ListView which needs to be handled slightly differently when it comes to aligning/sizing. Check this previous SO question for an answer on how to do this in XAML: In WPF Stretch a control to fill a ListView Column

I'm a bit confused why the other response is marked as the answer. The problem is that the header container is not stretched (if you scan the WPF visual tree you can see that the ContentPresenter is not Stretch , it is Center instead).

If you want to stretch the headers so that the content shown in the header is takes up all the header space then you need to override the GridViewColumnHeader style.

<ListView HorizontalContentAlignment="Stretch" ...>
    <ListView.Resources>
        <Style x:Key="StretchedHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.Resources>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Width="200" HeaderContainerStyle="{StaticResource StretchedHeaderStyle}" DisplayMemberBinding="{Binding Path=SomeValue, Mode=OneWay"/>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

The above code sample will produce a list view that stretches into its container. It has a static resource header stretching style (must be applied to each column you want stretched in this case, remove the x:Key and the HeaderContainerStyle= to make all headers stretched). It also globally applies a stretching style to all column cells (this could be applied to individual columns if desired similar to how the header style is displayed).

If you needed to do this in code behind you would just create the style(s) and set the appropriate style property on the columns ( HeaderContainerStyle ).

UPDATE

I just realized there is a much better way to globally apply the header style. Instead of declaring the type applied style as a ListView resource, it is much better to apply the style directly to the GridView .

<ListView HorizontalContentAlignment="Stretch" ...>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridView.ColumnHeaderContainerStyle>
                <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </GridView.ColumnHeaderContainerStyle>
            <GridView.Columns>
                <GridViewColumn Width="200" DisplayMemberBinding="{Binding Path=SomeValue, Mode=OneWay"/>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

Both methods achieve the same result, but this method above is a much cleaner approach because you are assigning the style property directly responsible for header container style. However, if you only need to stretch specific columns then the first method is your best bet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM