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.
I’m not sure why the
HorizontalAlignmentand theHorizontalContentAlignmentproperties 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
RibbonGroupandGridto theRibbonTab'sActualWidthproperty. You might need to use a converter to fine tune these values with an offset, but all in all, it does the trick: