Grid.IsSharedScopeSize incompatible with * columns in WPF Grid

849 views Asked by At

I am using IsSharedSizeScope in an ItemsControl in WPF to keep the same widths for each row.

Unfortunately this isn't compatible with Width='*' columns, which makes the 'A B C' text column spill off the end of the page.

<Border BorderBrush="Red" BorderThickness="1">
    <StackPanel Grid.IsSharedSizeScope="True">

        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="G1"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="G2" />
                <ColumnDefinition Width="*"  SharedSizeGroup="G3" />
            </Grid.ColumnDefinitions>

            <TextBlock Text="Col0" Grid.Column="0" Margin="0,0,5,0"/>
            <TextBlock Text="Col1" Grid.Column="1" Margin="0,0,5,0"/>

            <TextBlock Text="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" TextWrapping="Wrap" Grid.Column="2"/> 
        </Grid>

    </StackPanel>                   

</Border>

This won't wrap, but if you change Grid.IsSharedScopeSize to false then it does work, but the rows are uneven.

Is there any clever way to achieve this, or will I have to manually set a width on the 'A B C' text column?

(Note: This is actually inside an ItemsControl not shown)

1

There are 1 answers

0
Simon_Weaver On

I just realized I don't need to set SharedSizeGroup for every column. By not setting it for the * column I can achieve the wrapping I need while maintaining consistent column widths for the other columns.

Unfortunately I don't think this will work for * columns that aren't the last column in the grid but I haven't tried yet.