2010-11-14 101 views
1

我想知道如何将命令附加到RibbonMenuButton项目。 以下是我的初始尝试,但命令从未被调用。如何将命令附加到wpf RibbonMenuButton?

<ribbon:RibbonWindow x:Class="RibbonMenuDemo.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"> 

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

     <ribbon:Ribbon x:Name="Ribbon"> 

      <ribbon:RibbonTab x:Name="HomeTab" 
           Header="Home"> 
       <ribbon:RibbonGroup x:Name="Group1" 
            Header="Map"> 
        <ribbon:RibbonMenuButton ItemsSource="{Binding Regions}" 
             LargeImageSource="Images\LargeIcon.png" 
             Label="Regions" > 
         <ribbon:RibbonMenuButton.ItemContainerStyle > 
          <Style TargetType="MenuItem" > 
           <Setter Property="Command" Value="{Binding RegionChangeCommand}" /> 
           <Setter Property="CommandParameter" Value="{Binding Label}"></Setter> 
          </Style> 
         </ribbon:RibbonMenuButton.ItemContainerStyle> 
        </ribbon:RibbonMenuButton> 
       </ribbon:RibbonGroup> 

      </ribbon:RibbonTab> 
     </ribbon:Ribbon> 
    </Grid> 
</ribbon:RibbonWindow> 

这里是我的代码

using System.Windows.Navigation; 
using System.Windows.Shapes; 
using Microsoft.Windows.Controls.Ribbon; 
using System.ComponentModel; 
using System.Diagnostics; 

namespace RibbonMenuDemo 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : RibbonWindow 
    { 
     RelayCommand regionChangeCommand; 


     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new Map(); 
     } 

     public RelayCommand RegionChangeCommand 
     { 
      get 
      { 
       if (regionChangeCommand == null) 
        regionChangeCommand = new RelayCommand(param => OnRegionChange(param), param => false); 

       return regionChangeCommand; 
      } 
     } 
     private void OnRegionChange(object param) 
     { 
      var val = (string)param; 
      MessageBox.Show(val); 
     } 
    } 

    public class Map 
    { 
     public Map() 
     { 
      Regions = new List<string> 
      { 
       "EAST", "North", "West", "South" 
      }; 
     } 
     public List<string> Regions 
     { 
      get; 
      set; 
     } 
    } 

    public class RelayCommand : ICommand 
    { 
     readonly Action<object> execute; 
     readonly Predicate<object> canExecute; 

     /// <summary> 
     /// create new simple command 
     /// </summary> 
     /// <param name="execute">execute handler</param> 
     /// <param name="canExecute">predicate to determin if can excute</param> 
     public RelayCommand(Action<object> execute, Predicate<object> canExecute = null) 
     { 
      if (execute == null) 
       throw new ArgumentNullException("execute handler required"); 

      this.execute = execute; 
      Predicate<object> v = (x) => { return true; }; 
      this.canExecute = canExecute ?? v; 
     } 

     public void Execute(object parameter) 
     { 
      this.execute(parameter); 
     } 
     [DebuggerStepThrough] 
     public bool CanExecute(object parameter) 
     { 
      return canExecute(parameter); 
     } 
     public event EventHandler CanExecuteChanged 
     { 
      add { CommandManager.RequerySuggested += value; } 
      remove { CommandManager.RequerySuggested -= value; } 
     } 
    } 
} 

回答

1

解决这样的:

<ribbon:RibbonMenuButton DataContext="{Binding .}" ItemsSource="{Binding Regions}" 
    LargeImageSource="Images\LargeIcon.png" Label="Regions"> 
    <ribbon:RibbonMenuButton.ItemContainerStyle> 
     <Style TargetType="MenuItem"> 
      <Setter Property="Command" Value="{Binding DataContext.RegionChangeCommand, 
       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type 
       ribbon:RibbonMenuButton}}}" /> 
      <Setter Property="CommandParameter" Value="{Binding Label}"></Setter> 
     </Style> 
    </ribbon:RibbonMenuButton.ItemContainerStyle> 
</ribbon:RibbonMenuButton> 

Click event routing on RibbonButton under RibbonMenuButton?页上的MSDN在Visual Studio论坛改编。

+0

-1用于应对此问题的答案[RibbonMutuButton下的RibbonButton上的单击事件路由?](http://social.msdn.microsoft.com/Forums/vstudio/zh-cn/177da553-3e7b-4ffa-806d-7562a65748b0 /点击事件路由上ribbonbutton-下ribbonmenubutton?论坛= WPF)。你在不给作者信任的情况下使用别人的作品。这相当于抄袭,并且Stack Overflow不受欢迎。请记得在使用其他来源时始终添加显着的归因。 – Sheridan 2014-03-25 16:27:04