【问题标题】:Dropdown like button on menu tab in Windows 7Windows 7中菜单选项卡上的下拉按钮
【发布时间】:2011-12-07 00:26:50
【问题描述】:

我只是好奇是否有人知道如何使用 WPF 在 Windows 7 资源管理器选项卡上执行类似下拉菜单的按钮(请参见下面的截图)。

【问题讨论】:

    标签: c# .net wpf xaml wpf-controls


    【解决方案1】:

    我使用这个类,它看起来很棒: http://andyonwpf.blogspot.com/2006/10/dropdownbuttons-in-wpf.html

    我将在这里发布,以便阅读更清晰:

    /// <summary>
    /// Andy On WPF: DropDownButtons in WPF
    /// http://andyonwpf.blogspot.com/2006/10/dropdownbuttons-in-wpf.html
    /// </summary>
    public class DropDownButton : ToggleButton
    {
        #region Members
    
        public enum Placement { Bottom, Right }
    
        #endregion
    
        #region Properties
    
        #region DropDownPlacement
    
        /// <summary>
        /// DropDown placement.
        /// </summary>
        public Placement DropDownPlacement
        {
            get { return (Placement)GetValue(DropDownPlacementProperty); }
            set { SetValue(DropDownPlacementProperty, value); }
        }
    
        /// <summary>
        /// DropDown placement (Dependency Property).
        /// </summary>
        public static readonly DependencyProperty DropDownPlacementProperty =
            DependencyProperty.Register("DropDownPlacement", typeof(Placement),
            typeof(DropDownButton), new UIPropertyMetadata(null));
    
        #endregion
    
        #region DropDown
    
        /// <summary>
        /// DropDown property.
        /// </summary>
        public ContextMenu DropDown
        {
            get { return (ContextMenu)GetValue(DropDownProperty); }
            set { SetValue(DropDownProperty, value); }
        }
    
        /// <summary>
        /// DropDown property (Dependency property).
        /// </summary>
        public static readonly DependencyProperty DropDownProperty =
            DependencyProperty.Register("DropDown", typeof(ContextMenu),
            typeof(DropDownButton), new PropertyMetadata(null, OnDropDownChanged));
    
        #endregion
    
        #endregion
    
        #region Events
    
        private static void OnDropDownChanged(DependencyObject sender,
            DependencyPropertyChangedEventArgs e)
        {
            ((DropDownButton)sender).OnDropDownChanged(e);
        }
    
        void OnDropDownChanged(DependencyPropertyChangedEventArgs e)
        {
            if (DropDown != null)
            {
                DropDown.PlacementTarget = this;
    
                switch (DropDownPlacement)
                {
                    default:
                    case Placement.Bottom:
                        DropDown.Placement = PlacementMode.Bottom;
                        break;
                    case Placement.Right:
                        DropDown.Placement = PlacementMode.Right;
                        break;
                }
    
                this.Checked +=
                    new RoutedEventHandler((a, b) => { DropDown.IsOpen = true; });
                this.Unchecked +=
                    new RoutedEventHandler((a, b) => { DropDown.IsOpen = false; });
                DropDown.Closed +=
                    new RoutedEventHandler((a, b) => { this.IsChecked = false; });
            }
        }
    
        #endregion
    }
    

    【讨论】:

    • 我认为这是最体面的方式。谢谢:)
    【解决方案2】:

    它有一个自定义的control template,它看起来有一个透明的背景,除非鼠标悬停,当然渐变和边框也不同。

    在 MSDN 上有 an example 用于自定义模板,它会导致一个相当蓝色的按钮,基本上你可以对模板做任何事情,但它们的创建可能是相当多的工作。 Expression Blend 有助于控制模板。

    【讨论】:

    • 是的,我已经知道这是一个控件模板,但是您知道这里将使用什么 ControlElemets 吗:)
    • 它可以是各种控件,我认为按钮的default template 可能是基本外观的良好开端,但您可能想要模板化MenuMenuItem,因为的功能。如果您知道如何创建一个包含状态、PART 等知识的模板。您应该能够重新创建它,这并不像您需要忠实于 Windows 中使用的实际内部结构。
    • 你说得有道理:),以我自己的方式重新创建这个模板可以解决这个问题。但是我仍然很好奇这里的微软人可以使用什么。无论如何谢谢你+1
    【解决方案3】:

    要获取下拉菜单部分,您可以设置按钮的 ContextMenu 属性,然后使用 ContextMenu.Placement 将其正确定位在按钮下方。

    您可能还必须设置 ContextMenu.PlacementTarget 以使其相对于 Button。

    【讨论】:

    • 嗯..这也是一个好主意,也许路由事件可能会通过这种方法有所帮助..谢谢你+1
    • 你可以实现Button的Click事件,让菜单在鼠标左键点击时下拉。
    • 我不知道这个,它在概念上是一个菜单,按钮是顶级菜单项,只是看起来有点不同,我不会弯曲按钮来提供它不应该的功能为。
    【解决方案4】:

    抱歉再次添加,但我刚刚意识到这个控件也存在于 Codeplex 上的扩展 WPF 工具包中:

    http://wpftoolkit.codeplex.com/wikipage?title=DropDownButton

    它是这样实现的(根据他们的网站):

    <extToolkit:DropDownButton Content="Click Me" Margin="15" >
        <extToolkit:DropDownButton.DropDownContent>
            <extToolkit:ColorCanvas />
        </extToolkit:DropDownButton.DropDownContent>
    </extToolkit:DropDownButton>
    

    您可以根据需要在其中添加 MenuItems。

    我已经将此套件用于其他功能(ChildWindow 和 SplitButton),我认为它做得很好。随着人们不断要求 WPF 中提供更多 Office 2007 / 2010 功能,CodePlex 和 Microsoft 会继续期待更多这样的功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多