Search code examples
wpfxamlribbonalignment

Stretch WPF grid horizontally inside RibbonGroup


How do I stretch a WPF Grid horizontally inside a RibbonGroup? It's not stretching despite the HorizontalAlignment property.

<Window x:Name="FrmMain" x:Class="PhotoCell.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="PhotoCell" Height="386.821" Width="757.701" WindowState="Maximized">
<Grid>
    <Image x:Name="imgMain" Margin="10,142,10,9"/>
    <Ribbon VerticalAlignment="Top">
        <RibbonTab Header="Home" Height="88" VerticalAlignment="Top">
            <RibbonGroup Header="Save/Load" Height="88" Margin="0" VerticalAlignment="Top" Width="90">
                <Grid HorizontalAlignment="Stretch">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <RibbonButton x:Name="cmdLoadImage" Background="#FFFFC500" Label="Load" Click="cmdLoadImage_Click" Margin="10,10,10,0" Grid.Row="0" HorizontalAlignment="Stretch" />
                    <RibbonButton x:Name="cmdSaveImage" Background="#FFFFC500" Label="Save" Click="cmdSaveImage_Click" Margin="10,10,10,0" Grid.Row="1" HorizontalAlignment="Stretch" />
                </Grid>
            </RibbonGroup>
        </RibbonTab>
    </Ribbon>
</Grid>
</Window>

I've also tried adding

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

After the row definitions.


Solution

  • I'm not sure why the HorizontalAlignment and the HorizontalContentAlignment properties aren't performing as they should - might be a bug in the ribbon's control templates.

    Anyway, there is a workaround: you could bind the RibbonGroup and Grid to the RibbonTab's ActualWidth property. You might need to use a converter to fine tune these values with an offset, but all in all, it does the trick:

    <Ribbon VerticalAlignment="Top">
        <RibbonTab x:Name="tab" Header="Home" Height="88" VerticalAlignment="Top">
            <RibbonGroup Width="{Binding ElementName=tab, Path=ActualWidth}" Header="Save/Load" Height="88" Margin="0" VerticalAlignment="Top">
                <Grid Width="{Binding ElementName=tab, Path=ActualWidth}" HorizontalAlignment="Stretch">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <RibbonButton x:Name="cmdLoadImage" Background="#FFFFC500" Label="Load" Click="cmdLoadImage_Click" Margin="10,10,10,0" Grid.Row="0" HorizontalAlignment="Stretch" />
                    <RibbonButton x:Name="cmdSaveImage" Background="#FFFFC500" Label="Save" Click="cmdSaveImage_Click" Margin="10,10,10,0" Grid.Row="1" HorizontalAlignment="Stretch" />
                </Grid>
            </RibbonGroup>
        </RibbonTab>
    </Ribbon>