WPF Ribbon Control - Change Content of a Ribbon Button
Is there any way to change the Content of a RibbonButton?
I know that a RibbonButton has an Image and a Label but I want a button with a shape (Rectangle class) in it's Content and this button should have the RibbonButton style applied. Is there a开发者_如何学Gony way to do that?
Why don't you create your own button control?
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" />
Code behind:
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);
}
}
精彩评论