1

有没有办法改变 RibbonButton 的内容?

我知道 RibbonButton 有一个 Image 和一个 Label,但我想要一个在它的 Content 中有一个形状(矩形类)的按钮,并且这个按钮应该应用 RibbonButton 样式。有没有办法做到这一点?

4

1 回答 1

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);
    }
}
于 2010-05-20T19:42:02.840 回答