Search code examples
c#wpfgridissharedsizescope

WPF - width issue with IsSharedSizeScope "true" and three columns


Simple code:

<ItemsControl Grid.IsSharedSizeScope="True" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="firstColumn" Width="Auto"/>
                    <ColumnDefinition SharedSizeGroup="splitterColumn" Width="Auto"/>
                    <ColumnDefinition SharedSizeGroup="lastColumn" Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Path=Key}"/>
                <GridSplitter Width="5" Grid.Column="1" />
                <TextBlock Text="{Binding Path=Value}" Grid.Column="2"/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Problem is occured with lastColumn, width of it is not 100%. I try to set HorizontalAlignment="Stretch" for the all grids but it didn't help.

Width of lastColumn must be 100%. How to resolve this issue?


Solution

  • You don't need to include SharedSizeGroup on every column. In this case, if your first and second columns are sharing a size, you should be able to get your desired behaviour by omitting the property on the last column. There should always be the same amount of space for the last column, if the first two columns are sharing sizes:

    <Grid Width="300" Height="30" Background="Red">
        <Grid Grid.IsSharedSizeScope="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="firstColumn" Width="Auto"/>
                <ColumnDefinition SharedSizeGroup="splitterColumn" Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBox Text="1" Grid.Column="0"/>
            <GridSplitter Width="5" Grid.Column="1"/>
            <TextBox Text="Test" Grid.Column="2" />
        </Grid>
    </Grid>
    

    Edit: This is essentially the problem encountered here: Grid.IsSharedScopeSize incompatible with * columns in WPF Grid

    Further edit: You've changed to an ItemsControl now, but the same should still apply.