如何在WPF功能区ApplicationMenu中更改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>
答案 0 :(得分:2)
如果您正在寻找一个非常快速的方法来增加高度,您可以简单地添加一些无用的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>
答案 1 :(得分:2)
根据这个帖子中的答案,我发现将RibbonApplicationMenu子类化并将第三列的Width设置为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;
}
}
现在您只需要从
更改Ribbon xaml<Ribbon.ApplicationMenu>
<RibbonApplicationMenu>
到
<Ribbon.ApplicationMenu>
<ctrl:CustomRibbonApplicationMenu>
答案 2 :(得分:1)
我搜索了同样问题的解决方案。
没有直接属性可以修改它。
可以在以下位置找到创建此类属性的示例 msdn
这是主要的解决方案:
更改功能区库的源代码。 MS提供了功能区库的源代码: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>
仅添加给定xaml的前两行
答案 3 :(得分:1)
这是一个老话,是的,它有一些好主意,但我并不满意。
我的问题略有不同,因为我需要ApplicationMenu才能扩展到足以适应辅助窗格中放置的任何控件。
最终我挖得很深,找到了一个我满意的解决方案。它没有解决&#34;填满屏幕&#34;问题,但我希望这会帮助那些降落在这里的人,寻找解决类似我的问题的方法。对不起,看起来我试图劫持线程。我不打算。
基本上我通过更改功能区样式解决了固定的宽度和高度问题:
此时您可能会问,&#34;为什么不使用VS或Blend或ShowMeTheTemplate而不是DotPeek?&#34;所有这些工具在带状组件上都失败了。不知道为什么。他们没有说。使用DotPeek的一个问题是某些命名空间引用需要调整,但它并不太难,所以我不会在这里详细介绍。
所以,既然你拥有了所有的样式和模板,那么去寻找有问题的标记。
首先,修正宽度:
<ColumnDefinition Width="300"/>
。只有一个。"300"
更改为"Auto"
。然后修正高度:
PART_SubmenuPlaceholder
边框的定义。您可以搜索x:Name="PART_SubmenuPlaceholder"
。它比你对宽度的改变低了大约50行。Height="{Binding ElementName=MainPaneBorder, Path=ActualHeight}"
。现在您已经修改了样式,只需将此资源字典添加到您的xaml中,它就应该将自身应用于功能区。
答案 4 :(得分:0)
当我遇到这个答案时(在搜索我自己的答案时,略有不同的问题),我对实际修改 Microsoft代码并不太兴奋。
因此,我更喜欢将其子类化,并使用相关“PART _...”上的base.GetTemplateChild
来获取必要的UI元素。
我想你可以采用类似的方法来实现你所需要的。
我的例子是here。
希望这有帮助。
附:如果您碰巧找到了确定AuxiliaryPanel必要宽度的方法,请告诉我 - 我想看看它是否也适用于菜单的宽度。
答案 5 :(得分:0)
您可以下载用于WPF源代码的Microsoft功能区(http://www.microsoft.com/en-us/download/details.aspx?id=11877)并向ApplicationMenu添加DependencyProperty Width / Height,或者像我的示例中那样“快速而又脏”:
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>
度过愉快的一天