【问题标题】:WPF Ribbon Control - Change Content of a Ribbon ButtonWPF 功能区控件 - 更改功能区按钮的内容
【发布时间】:2010-04-07 14:57:19
【问题描述】:

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

我知道 RibbonButton 有一个 Image 和一个 Label,但我想要一个在其内容中具有形状(矩形类)的按钮,并且该按钮应该应用 RibbonButton 样式。有什么办法吗?

【问题讨论】:

    标签: wpf button ribbon


    【解决方案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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      相关资源
      最近更新 更多