WPF功能区控件 - 更改功能区按钮的内容

时间:2010-04-07 14:57:19

标签: wpf button ribbon

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

我知道RibbonButton有一个Image和一个Label但是我想要一个带有形状(Rectangle类)的按钮,它的内容和这个按钮应该应用了RibbonButton样式。有没有办法做到这一点?

1 个答案:

答案 0 :(得分: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);
    }
}