2011-07-20 28 views
2

如何更改WPF Ribbon ApplicationMenu中AuxilaryPane的大小?我在該區域添加了最近的文件列表,但它被截斷。理想情況下,我希望輔助窗格像Word/Excel一樣填充屏幕。Ribbon ApplicationMenu AuxilaryPane尺寸

我的代碼:

<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

通過截斷我的意思是輔助的窗格不夠寬,以適應我的文件名。 – KrisTrip

回答

1

我搜索過相同的問題的解決方案。

沒有直接的屬性來修改這個。創建這種屬性的

示例可在 msdn

找到這裏的主要解決方案:

更換色帶庫的源代碼。 MS提供了功能區庫的源代碼:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=2bfc3187-74aa-4154-a670-76ef8bc2a0b4

下載源代碼,並打開它,在MicrosoftRibbonForWPFSourceAndSamples \ RibbonControlsLibrary \微軟\的Windows \控制\絲帶\ 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 \主題\ 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> 

只添加給定的XA的前兩行毫升

+0

你應該在你的答案中包含鏈接文章的主要部分__。你知道 - 如果微軟宣佈破產,他們的網站下線.. ;-)參見[答案] - 「爲鏈接提供的上下文」 – Arsen7

+0

增加外部鏈接 – Lif3styl3

0

當我遇到這個答案(同時尋找一個我自己的答案,稍微不同的問題),我並不太興奮,實際上修改微軟的代碼。因此,我更願意將其繼承,並在相關的「PART _...」上使用base.GetTemplateChild獲得所需UI元素的保留。
我想你可以按照類似的方法來實現你所需要的。 我的例子是here

希望這會有所幫助。
P.S.如果您碰巧找到一種方法來確定輔助面板的必要寬度,請告訴我 - 我想看看這是否適用於菜單的寬度。

1

如果您正在尋找快速修復來增加高度,可以簡單地添加一些無用的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> 
+0

哇,真是一個奇怪的黑客,這是必要的代碼。這不幸是我找到的最好的答案,所以我贊成它。 –

0

,你可以下載微軟絲帶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> 

有一個愉快的一天

1

這是一個古老的線程,是的,它有一些很好的想法,但我並不滿足。

我的問題稍有不同,因爲我需要ApplicationMenu才能展開以適應放置在輔助窗格中的任何控件。

最終我挖得很深,找到了一個我很滿意的解決方案。它並沒有解決「填滿屏幕」的問題,但我希望這將有助於其他人降落在這裏,尋找類似於我的問題的解決方案。對不起,如果它看起來像我試圖劫持線程。我不打算。

基本上我通過改變帶式解決了固定的寬度和高度的問題:

  • 打開帶組件在JetBrains的DotPeek
  • 打開資源/ System.Windows.Controls.Ribbon.g.resources /主題/ generic.baml
  • 將整個資源字典複製到項目中的.xaml文件中。你可能只能使用它的一部分,但我決定採取一切。

此時您可能會問:「爲什麼不使用VS或Blend或ShowMeTheTemplate而不是DotPeek?」所有這些工具都在色帶組件上失敗了。不知道爲什麼。他們沒有說。使用DotPeek的一個問題是,某些名稱空間引用需要調整,但這並不困難,所以在這裏我不會詳細討論。

所以,現在你有所有的樣式和模板,去尋找有問題的標記。

首先,固定寬度:

  • 查找其第三列的定義是300的靜態值可以搜索<ColumnDefinition Width="300"/>電網。只有一個。
  • "300"更改爲"Auto"

然後修復高度:

  • 查找PART_SubmenuPlaceholder邊界的定義。您可以搜索x:Name="PART_SubmenuPlaceholder"。它在寬度的變化下面大約有50行。
  • 該邊框將Height屬性綁定到「MainPaneBorder」控件的ActualHeight屬性:Height="{Binding ElementName=MainPaneBorder, Path=ActualHeight}"
  • 刪除此高度定義。

現在您已經修改了樣式,只需將此資源字典添加到您的xaml中,並將其應用於功能區即可。

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.ApplicationMenu> 
       <RibbonApplicationMenu> 

改變你的絲帶XAML來

<Ribbon.ApplicationMenu> 
       <ctrl:CustomRibbonApplicationMenu> 
+0

thx ..這是修復低劣絲帶東西的一步。 – LuckyLikey