2010-04-07 91 views
1

有什么方法可以更改RibbonButton的内容吗?WPF Ribbon控件 - 更改功能区按钮的内容

我知道一个RibbonButton有一个图像和一个标签,但我想要一个带有形状(Rectangle类)的按钮,它的内容中并且此按钮应该具有应用的RibbonButton样式。有没有办法做到这一点?

回答

1

为什么不创建自己的按钮控件?

XAML:

<Button x:Class="MyApp.myButton" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="71" d:DesignWidth="87"> 
    <Rectangle Height="40" Width="40" Fill="#FFC11414" /> 

背后代码:

public partial class myButton : Button, IRibbonControl 
{ 

    public myButton() 
    { 
     InitializeComponent(); 
     Type forType = typeof(myButton); 
     FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(forType, new FrameworkPropertyMetadata(forType)); 
     ButtonBase.CommandProperty.OverrideMetadata(forType, new FrameworkPropertyMetadata(new PropertyChangedCallback(OnCommandChanged))); 
     FrameworkElement.ToolTipProperty.OverrideMetadata(forType, new FrameworkPropertyMetadata(null, new CoerceValueCallback(RibbonButton.CoerceToolTip))); 
     ToolTipService.ShowOnDisabledProperty.OverrideMetadata(forType, new FrameworkPropertyMetadata(true)); 
    } 
    public static object CoerceToolTip(DependencyObject d, object value) 
    { 
     if (value == null) 
     { 
      RibbonButton button = (RibbonButton)d; 
      RibbonCommand command = button.Command as RibbonCommand; 
      if ((command == null) || ((string.IsNullOrEmpty(command.ToolTipTitle) && string.IsNullOrEmpty(command.ToolTipDescription)) && (command.ToolTipImageSource == null))) 
      { 
       return value; 
      } 
      value = new RibbonToolTip(command); 
     } 
     return value; 
    } 
    private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     ((myButton)d).CoerceValue(FrameworkElement.ToolTipProperty); 
    } 
}