在RibbonGroup内水平拉伸WPF网格

时间:2012-11-17 11:20:07

标签: wpf xaml ribbon alignment

如何在Grid内水平拉伸WPF RibbonGroup?尽管有HorizontalAlignment属性,但它并没有延伸。

<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>

我也尝试过添加

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

在行定义之后。

2 个答案:

答案 0 :(得分:3)

我不确定为什么HorizontalAlignmentHorizontalContentAlignment属性没有按预期执行 - 可能是功能区控件模板中的错误。

无论如何,有一种解决方法:您可以将RibbonGroupGrid绑定到RibbonTab's ActualWidth属性。您可能需要使用转换器来使用偏移来微调这些值,但总而言之,它可以解决这个问题:

<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>

答案 1 :(得分:0)

我希望RibbonGroup内有Ribbon个拉伸。我用了不同的方法。由于我必须做一些计算(得到所有其他RibbonGroups的宽度),我没有使用带转换器的Binding。在转换器中,我无法轻松到达其他控件。我使用了SizeChanged的{​​{1}}事件。在这里,我设置了Ribbon

的宽度
RibbonGroup

private void RibbonOnSizeChanged(object sender, SizeChangedEventArgs sizeChangedEventArgs) { ChangeWidgetsGroupWidth(sizeChangedEventArgs.NewSize.Width); } private void ChangeWidgetsGroupWidth(double newRibbonWidth) { double otherRibbonGroupWidths = 0; foreach (RadRibbonGroup item in HomeTab.Items) { if (item != rgWidgetsGroup) { otherRibbonGroupWidths += item.ActualWidth + 2; // Extra margin between groups } } double width = newRibbonWidth - otherRibbonGroupWidths - 6; // '6' to add extra margin rgWidgetsGroup.Width = width > 0 ? width : 0; } 是我希望拥有最大尺寸的rgWidgetsGroup。我认为对RibbonGroup内的Control做类似的事情很容易。