3

如何在一个 WPFGrid内水平拉伸 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>

在行定义之后。

4

2 回答 2

3

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

无论如何,有一个解决方法:您可以将RibbonGroupand绑定Grid到该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>
于 2012-11-17T13:41:17.837 回答
0

我想RibbonGroupRibbon. 我使用了不同的方法。由于我必须进行一些计算(获取所有其他 RibbonGroups 的宽度),因此我没有使用带转换器的绑定。在转换器中,我无法轻松访问其他控件。我使用SizeChangedRibbon. 这里我设置了宽度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;
    }

rgWidgetsGroupRibbonGroup我想要的最大尺寸。Control我认为对inside a做类似的事情会很容易RibbonGroup

于 2012-12-07T14:49:38.360 回答