开发者

Autosize header in WPF HeaderedItemsControl

I'm using a HeaderedItemsControl to show pers开发者_StackOverflowon names. I also want to show a header that contains 3 labels: Title, First Name and Last Name. This is easy when the names are short. However, when there is a very long firstname, the header's don't match the names anymore. How can I fix this? Thank you!


Not sure if you really want to use this class:

A HeaderedItemsControl has a limited default style. To create a HeaderedItemsControl with a custom appearance, create a new ControlTemplate.

Anyway, to line up stuff you can use Grids with shared size, e.g.:

<HeaderedItemsControl ItemsSource="{Binding Data}" Grid.IsSharedSizeScope="True">
    <HeaderedItemsControl.Template>
        <ControlTemplate TargetType="HeaderedItemsControl">
            <StackPanel>
                <ContentPresenter ContentSource="Header" />
                <ItemsPresenter  />
            </StackPanel>
        </ControlTemplate>
    </HeaderedItemsControl.Template>
    <HeaderedItemsControl.Header>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="A" />
                <ColumnDefinition SharedSizeGroup="B" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="Name" />
            <TextBlock Grid.Column="1" Text="Occupation" />
        </Grid>
    </HeaderedItemsControl.Header>
    <HeaderedItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="A" />
                    <ColumnDefinition SharedSizeGroup="B" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Name}"/>
                <TextBlock Grid.Column="1" Text="{Binding Occupation}" />
            </Grid>
        </DataTemplate>
    </HeaderedItemsControl.ItemTemplate>
</HeaderedItemsControl>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜