功能区应用菜单辅助窗格大小

发布于 2024-11-25 10:31:52 字数 1246 浏览 4 评论 0原文

如何更改 WPF 功能区应用程序菜单中 AuxilaryPane 的大小?我已将最近的文件列表添加到该区域,但它被截断了。理想情况下,我希望辅助窗格像 Word/Excel 一样填满屏幕。

我的代码:

<r:Ribbon.ApplicationMenu>
    <r:RibbonApplicationMenu>
        <r:RibbonApplicationMenu.AuxiliaryPaneContent>
            <StackPanel>
                <TextBlock Text="Recent Files" />
                <Separator />
                <ItemsControl ItemsSource="{Binding RecentFiles}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <r:RibbonApplicationMenuItem Header="{Binding ShortPath}" 
                                                         Command="{Binding DataContext.OpenRecentFileCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                                         CommandParameter="{Binding LongPath}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </r:RibbonApplicationMenu.AuxiliaryPaneContent>
    </r:RibbonApplicationMenu>                
</r:Ribbon.ApplicationMenu>

How do I change the size of the AuxilaryPane in the WPF Ribbon ApplicationMenu? I have added a recent file list to that area but it is getting truncated. Ideally I'd like the auxilary pane to fill the screen like it does for Word/Excel.

My code:

<r:Ribbon.ApplicationMenu>
    <r:RibbonApplicationMenu>
        <r:RibbonApplicationMenu.AuxiliaryPaneContent>
            <StackPanel>
                <TextBlock Text="Recent Files" />
                <Separator />
                <ItemsControl ItemsSource="{Binding RecentFiles}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <r:RibbonApplicationMenuItem Header="{Binding ShortPath}" 
                                                         Command="{Binding DataContext.OpenRecentFileCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                                         CommandParameter="{Binding LongPath}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </r:RibbonApplicationMenu.AuxiliaryPaneContent>
    </r:RibbonApplicationMenu>                
</r:Ribbon.ApplicationMenu>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦一生花开无言 2024-12-02 10:31:53

如果您正在寻找一种非常快速的解决方案来增加高度,您可以简单地添加一些无用的 RibbonApplicationMenuItems 来填充框(并且不必修改 MS 源代码)。

            <ribbon:Ribbon.ApplicationMenu>
            <ribbon:RibbonApplicationMenu>
                <ribbon:RibbonApplicationMenu.Items>
                    <ribbon:RibbonApplicationMenuItem Name="saveSettings" Header="Save Settings" />
                    <ribbon:RibbonApplicationMenuItem IsEnabled="False"/> <!--USELESS-->
                    <ribbon:RibbonApplicationMenuItem IsEnabled="False"/> <!--USELESS-->
                </ribbon:RibbonApplicationMenu.Items>

                <ribbon:RibbonApplicationMenu.AuxiliaryPaneContent >
                        <StackPanel Orientation="Vertical" >
                            <GroupBox>
                                <Label Content="System Settings" />
                            </GroupBox>
                            <StackPanel Orientation="Horizontal">
                            </StackPanel>
                        </StackPanel>
                </ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
            </ribbon:RibbonApplicationMenu>
        </ribbon:Ribbon.ApplicationMenu>

If you are looking for a very quick fix to increase the height, you can simply add some useless RibbonApplicationMenuItems to pad out the box (and not have to modify MS source code).

            <ribbon:Ribbon.ApplicationMenu>
            <ribbon:RibbonApplicationMenu>
                <ribbon:RibbonApplicationMenu.Items>
                    <ribbon:RibbonApplicationMenuItem Name="saveSettings" Header="Save Settings" />
                    <ribbon:RibbonApplicationMenuItem IsEnabled="False"/> <!--USELESS-->
                    <ribbon:RibbonApplicationMenuItem IsEnabled="False"/> <!--USELESS-->
                </ribbon:RibbonApplicationMenu.Items>

                <ribbon:RibbonApplicationMenu.AuxiliaryPaneContent >
                        <StackPanel Orientation="Vertical" >
                            <GroupBox>
                                <Label Content="System Settings" />
                            </GroupBox>
                            <StackPanel Orientation="Horizontal">
                            </StackPanel>
                        </StackPanel>
                </ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
            </ribbon:RibbonApplicationMenu>
        </ribbon:Ribbon.ApplicationMenu>
自找没趣 2024-12-02 10:31:53

我已经找到了同样问题的解决方案。

没有直接属性可以修改它。

创建此类属性的示例可以在以下位置找到:
msdn

这是主要解决方案:

更改Ribbon库的源代码。 MS 提供了 Ribbon 库的源代码:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=2bfc3187-74aa-4154-a670-76ef8bc2a0b4

下载源代码并打开,在MicrosoftRibbonForWPFSourceAndSamples\RibbonControlsLibrary\Microsoft\Windows\Controls\Ribbon\RibbonApplicationMenu.cs,添加一个依赖属性:

    public double MinMenuHeight
    {
      get { return (double)GetValue(MinMenuHeightProperty); }
      set { SetValue(MinMenuHeightProperty, value); }
    }
    public static readonly DependencyProperty MinMenuHeightProperty =
        DependencyProperty.Register("MinMenuHeight", typeof(double), typeof(RibbonApplicationMenu), new UIPropertyMetadata(0.0));

在 MicrosoftRibbonForWPFSourceAndSamples\RibbonControlsLibrary\Themes\Generic.xaml 的第 7519 行中,添加 XAML 代码:

    <Border x:Name="PopupBorder" MinHeight="{TemplateBinding MinMenuHeight}" BorderBrush="{Binding RelativeSource={RelativeSource AncestorType={x:Type ribbon:RibbonMenuButton}}, Path=Ribbon.BorderBrush}" Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type ribbon:RibbonMenuButton}}, Path=Ribbon.Background}" BorderThickness="1" CornerRadius="2">
        <Grid>

        </Grid>
    </Border>

仅添加给定的前两行数据文件

I've searched a solution for the same problem.

There is no direct property to modify this.

An Example of creating such property can be found at
msdn

here's the main solution:

Change the source code of the Ribbon Library. MS has provided the source code of the Ribbon Library: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=2bfc3187-74aa-4154-a670-76ef8bc2a0b4

Download the source code and open it, in the MicrosoftRibbonForWPFSourceAndSamples\RibbonControlsLibrary\Microsoft\Windows\Controls\Ribbon\RibbonApplicationMenu.cs, add one Dependency Property:

    public double MinMenuHeight
    {
      get { return (double)GetValue(MinMenuHeightProperty); }
      set { SetValue(MinMenuHeightProperty, value); }
    }
    public static readonly DependencyProperty MinMenuHeightProperty =
        DependencyProperty.Register("MinMenuHeight", typeof(double), typeof(RibbonApplicationMenu), new UIPropertyMetadata(0.0));

In the MicrosoftRibbonForWPFSourceAndSamples\RibbonControlsLibrary\Themes\Generic.xaml, line 7519, add the XAML code:

    <Border x:Name="PopupBorder" MinHeight="{TemplateBinding MinMenuHeight}" BorderBrush="{Binding RelativeSource={RelativeSource AncestorType={x:Type ribbon:RibbonMenuButton}}, Path=Ribbon.BorderBrush}" Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type ribbon:RibbonMenuButton}}, Path=Ribbon.Background}" BorderThickness="1" CornerRadius="2">
        <Grid>

        </Grid>
    </Border>

Only add the first two rows of the given xaml

猫腻 2024-12-02 10:31:53

是的,这是一个旧线程,它有一些好主意,但我不满意。

我的问题略有不同,因为我需要 ApplicationMenu 扩展至足以容纳放置在辅助窗格中的任何控件。

最终我深入挖掘并找到了一个令我满意的解决方案。它不能解决“填满屏幕”的问题,但我希望这能帮助那些登陆这里、寻找与我类似的问题的解决方案的人。抱歉,如果看起来我想劫持该线程。我无意。

本质上,我通过更改功能区样式解决了固定宽度和高度问题:

  • 在 JetBrains DotPeek 中打开功能区组件
  • 打开 Resources/System.Windows.Controls.Ribbon.g.resources/Themes/generic.baml
  • 将整个资源字典复制到 .项目中的 xaml 文件。你也许可以只使用其中的一部分,但我决定使用整个东西。

此时您可能会问,“为什么不直接使用 VS 或 Blend 或 ShowMeTheTemplate 而不是 DotPeek?”所有这些工具在色带组件上都惨遭失败。不知道为什么。他们没有说。使用DotPeek的问题之一是一些命名空间引用需要调整,但这并不是太困难,所以这里不再赘述。

因此,现在您已经拥有了所有样式和模板,接下来就可以查找有问题的标记了。

首先,固定宽度:

  • 查找第三列定义为静态值 300 的网格。您可以搜索 。只有一个。
  • “300”更改为“自动”

然后修复高度:

  • 查找PART_SubmenuPlaceholder边框的定义。您可以搜索x:Name="PART_SubmenuPlaceholder"。它比您对宽度所做的更改低大约 50 行。
  • 该 Border 将 Height 属性绑定到“MainPaneBorder”控件的 ActualHeight 属性:Height="{Binding ElementName=MainPaneBorder, Path=ActualHeight}"
  • 删除此高度定义。

现在您已经修改了样式,只需将此资源字典添加到您的 xaml 中,它就会应用到功能区。

This is an old thread, yes, and it has some good ideas, but I wasn't satisfied.

My problem was slightly different in that I needed the ApplicationMenu to expand only enough to fit any control that was placed in the auxiliary pane.

Eventually I dug deep and found a solution I was happy with. It doesn't solve the "fill the screen" problem, but I'm hoping this will help others who land here, looking for a solution to a problem similar to mine. Sorry if it looks like I'm trying to hijack the thread. I don't intend to.

Essentially I solved the fixed width and height problem by changing the ribbon style:

  • Open the ribbon assembly in JetBrains DotPeek
  • Open Resources/System.Windows.Controls.Ribbon.g.resources/Themes/generic.baml
  • Copy the entire resource dictionary into a .xaml file in your project. You may be able to get away with using only a part of it, but I decided to take the whole thing.

At this point you may be asking, "Why not just use VS or Blend or ShowMeTheTemplate instead of DotPeek?" All of these tools failed miserably on the ribbon assembly. Don't know why. They didn't say. One of the problems with using DotPeek is that some of the namespace references will need adjusting, but it's not too difficult, so I won't go into details here.

So, now that you have all the styles and templates, go look for the offending markup.

First, fix the width:

  • Look for the grid whose third column definition is a static value of 300. You can search for <ColumnDefinition Width="300"/>. There is only one.
  • Change the "300" to "Auto".

Then fix the height:

  • Look for the definition of PART_SubmenuPlaceholder Border. You can search for x:Name="PART_SubmenuPlaceholder". It is about 50 lines below the change you did for the width.
  • That Border binds the Height property to the ActualHeight property of the "MainPaneBorder" control: Height="{Binding ElementName=MainPaneBorder, Path=ActualHeight}".
  • Remove this Height definition.

Now that you've modified the style, just add this resource dictionary to your xaml and it should apply itself to the ribbon.

夜未央樱花落 2024-12-02 10:31:53

当我遇到这个答案时(在寻找我自己的略有不同问题的答案时),我对实际修改 Microsoft 代码并不太感兴趣。
因此,我宁愿对其进行子类化,并在相关的“PART_...”上使用 base.GetTemplateChild 获取必要的 UI 元素。
我想您可以遵循类似的方法来实现您所需要的。
我的示例位于此处

希望这有帮助。
PS 如果您碰巧找到一种方法来确定 AuxiliaryPanel 的必要宽度,请告诉我 - 我想看看这是否也适用于菜单的宽度。

When I came across this answer (while searching for an answer to my own, slightly different question), I wasn't too excited about actually modifying Microsoft code.
As a result, I rather preferred to subclass it, and get the hold of the necessary UI element using base.GetTemplateChild on the relevant "PART_...".
I suppose you can follow a similar approach to achieve what you need.
My example is here.

Hope this helps.
P.S. If you happen to find a way to determine the necessary width of the AuxiliaryPanel, please let me know - I would like to see if that's applicable to the menu's width as well.

醉殇 2024-12-02 10:31:53

您可以下载 Microsoft Ribbon for WPF 源代码 (http:// www.microsoft.com/en-us/download/details.aspx?id=11877)并将 DependencyProperty 宽度/高度添加到 ApplicationMenu 或只是“快速而肮脏”地进行在我的示例中:

MainWindow.xaml

public partial class MainWindow : RibbonWindow
{
    private Size DefaultApplicationMenuSize;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var grid = (myRibbon.ApplicationMenu.Template.FindName("MainPaneBorder", myRibbon.ApplicationMenu) as Border).Parent as Grid;
        /* before the first opening of the menu the size is NaN, so you have to measure size and use the DesiredSize */
        grid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        this.DefaultApplicationMenuSize = new Size(grid.ColumnDefinitions[2].Width.Value, grid.DesiredSize.Height);
    }

    private void RibbonApplicationMenuItem_MouseEnter(object sender, MouseEventArgs e)
    {           
        Button b=new Button();
        b.Content = "my epic button";
        b.Width = 500;
        b.Height = 500;
        b.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        SetApplicationMenuSize(b.DesiredSize);
        this.ribbonContentPresenter.Content = b;
    }

    private void RibbonApplicationMenuItem_MouseLeave(object sender, MouseEventArgs e)
    {
        SetApplicationMenuSize(DefaultApplicationMenuSize);
        this.ribbonContentPresenter.Content = null; 
    }

    private void SetApplicationMenuSize(Size size)
    {
        var grid = (myRibbon.ApplicationMenu.Template.FindName("MainPaneBorder", myRibbon.ApplicationMenu) as Border).Parent as Grid;
        /* you can modify the width of the whole menu */
        //grid.Width = size.Width;
        /* or just the size of RibbonApplicationMenu.AuxiliaryPaneContent */
        grid.ColumnDefinitions[2].Width = new GridLength(size.Width);
        grid.Height = size.Height;
    }
}

MainWindow.xaml.cs

<ribbon:RibbonWindow x:Class="WpfRibbonApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        Title="MainWindow"
        x:Name="RibbonWindow"
        Width="640" Height="480"
        Loaded="RibbonWindow_Loaded">

    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ribbon:Ribbon x:Name="myRibbon">
            <ribbon:Ribbon.ApplicationMenu>
                <ribbon:RibbonApplicationMenu SmallImageSource="Images\SmallIcon.png">
                    <ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon"
                                                      ImageSource="Images\LargeIcon.png"/>
                    <ribbon:RibbonApplicationMenuItem Header="HoverTest"
                                                      ImageSource="Images\LargeIcon.png"
                                                      MouseEnter="RibbonApplicationMenuItem_MouseEnter"
                                                      MouseLeave="RibbonApplicationMenuItem_MouseLeave" 
                                                      StaysOpenOnClick="True" />
                    <ribbon:RibbonApplicationMenu.FooterPaneContent>
                        <ribbon:RibbonButton Label="What ever" HorizontalAlignment="Right"/>
                    </ribbon:RibbonApplicationMenu.FooterPaneContent>
                    <ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
                        <ribbon:RibbonContentPresenter Name="ribbonContentPresenter" />
                    </ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
                </ribbon:RibbonApplicationMenu>
            </ribbon:Ribbon.ApplicationMenu>            
        </ribbon:Ribbon>         
    </Grid>
</ribbon:RibbonWindow>

度过了愉快的一天

You can download the Microsoft Ribbon for WPF Source Code (http://www.microsoft.com/en-us/download/details.aspx?id=11877) and add a DependencyProperty Width/Height to ApplicationMenu or just do it 'quick and dirty' like in my example:

MainWindow.xaml

public partial class MainWindow : RibbonWindow
{
    private Size DefaultApplicationMenuSize;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var grid = (myRibbon.ApplicationMenu.Template.FindName("MainPaneBorder", myRibbon.ApplicationMenu) as Border).Parent as Grid;
        /* before the first opening of the menu the size is NaN, so you have to measure size and use the DesiredSize */
        grid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        this.DefaultApplicationMenuSize = new Size(grid.ColumnDefinitions[2].Width.Value, grid.DesiredSize.Height);
    }

    private void RibbonApplicationMenuItem_MouseEnter(object sender, MouseEventArgs e)
    {           
        Button b=new Button();
        b.Content = "my epic button";
        b.Width = 500;
        b.Height = 500;
        b.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        SetApplicationMenuSize(b.DesiredSize);
        this.ribbonContentPresenter.Content = b;
    }

    private void RibbonApplicationMenuItem_MouseLeave(object sender, MouseEventArgs e)
    {
        SetApplicationMenuSize(DefaultApplicationMenuSize);
        this.ribbonContentPresenter.Content = null; 
    }

    private void SetApplicationMenuSize(Size size)
    {
        var grid = (myRibbon.ApplicationMenu.Template.FindName("MainPaneBorder", myRibbon.ApplicationMenu) as Border).Parent as Grid;
        /* you can modify the width of the whole menu */
        //grid.Width = size.Width;
        /* or just the size of RibbonApplicationMenu.AuxiliaryPaneContent */
        grid.ColumnDefinitions[2].Width = new GridLength(size.Width);
        grid.Height = size.Height;
    }
}

MainWindow.xaml.cs

<ribbon:RibbonWindow x:Class="WpfRibbonApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        Title="MainWindow"
        x:Name="RibbonWindow"
        Width="640" Height="480"
        Loaded="RibbonWindow_Loaded">

    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ribbon:Ribbon x:Name="myRibbon">
            <ribbon:Ribbon.ApplicationMenu>
                <ribbon:RibbonApplicationMenu SmallImageSource="Images\SmallIcon.png">
                    <ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon"
                                                      ImageSource="Images\LargeIcon.png"/>
                    <ribbon:RibbonApplicationMenuItem Header="HoverTest"
                                                      ImageSource="Images\LargeIcon.png"
                                                      MouseEnter="RibbonApplicationMenuItem_MouseEnter"
                                                      MouseLeave="RibbonApplicationMenuItem_MouseLeave" 
                                                      StaysOpenOnClick="True" />
                    <ribbon:RibbonApplicationMenu.FooterPaneContent>
                        <ribbon:RibbonButton Label="What ever" HorizontalAlignment="Right"/>
                    </ribbon:RibbonApplicationMenu.FooterPaneContent>
                    <ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
                        <ribbon:RibbonContentPresenter Name="ribbonContentPresenter" />
                    </ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
                </ribbon:RibbonApplicationMenu>
            </ribbon:Ribbon.ApplicationMenu>            
        </ribbon:Ribbon>         
    </Grid>
</ribbon:RibbonWindow>

have a nice day

缱倦旧时光 2024-12-02 10:31:52

根据此线程中的答案,我发现最简单的方法是子类化 RibbonApplicationMenu 并将第三列的宽度设置为自动。

 public class CustomRibbonApplicationMenu : System.Windows.Controls.Ribbon.RibbonApplicationMenu
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            System.Windows.DependencyObject obj = this.GetTemplateChild("PART_AuxiliaryPaneContentPresenter");

            System.Windows.Controls.ContentPresenter c = obj as System.Windows.Controls.ContentPresenter;

            ((System.Windows.Controls.Grid)((System.Windows.Controls.Border)c.Parent).Parent).ColumnDefinitions[2].Width = System.Windows.GridLength.Auto;
        }
    }

现在您只需将 Ribbon xaml 从 更改

<Ribbon.ApplicationMenu>
                <RibbonApplicationMenu>

<Ribbon.ApplicationMenu>
                <ctrl:CustomRibbonApplicationMenu>

Based on the answers in this thread I found it easiest to subclass RibbonApplicationMenu and set Width of the third column to Auto.

 public class CustomRibbonApplicationMenu : System.Windows.Controls.Ribbon.RibbonApplicationMenu
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            System.Windows.DependencyObject obj = this.GetTemplateChild("PART_AuxiliaryPaneContentPresenter");

            System.Windows.Controls.ContentPresenter c = obj as System.Windows.Controls.ContentPresenter;

            ((System.Windows.Controls.Grid)((System.Windows.Controls.Border)c.Parent).Parent).ColumnDefinitions[2].Width = System.Windows.GridLength.Auto;
        }
    }

Now you just need to change your Ribbon xaml from

<Ribbon.ApplicationMenu>
                <RibbonApplicationMenu>

to

<Ribbon.ApplicationMenu>
                <ctrl:CustomRibbonApplicationMenu>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文