How to open a popup menu when a button is clicked?

后端 未结 4 2009
时光取名叫无心
时光取名叫无心 2020-12-30 01:14

I have a button with an Image as its content in a toolbar. I would like this button to open a menu beneath it when clicked. How?


                     


        
4条回答
  •  盖世英雄少女心
    2020-12-30 02:02

    i found this two solutions after searching for it:

    1) Split Button in WPF

    2) DropDownButtons in WPF

    the second solution is my favorit (source taken from the website by Andrew Wilkinson)

    public class DropDownButton : ToggleButton
    {
      // *** Dependency Properties ***
    
      public static readonly DependencyProperty DropDownProperty =
        DependencyProperty.Register("DropDown",
                                    typeof(ContextMenu),
                                    typeof(DropDownButton),
                                    new UIPropertyMetadata(null));
    
      // *** Constructors *** 
    
      public DropDownButton() {
        // Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property 
    
        Binding binding = new Binding("DropDown.IsOpen");
        binding.Source = this;
        this.SetBinding(IsCheckedProperty, binding);
      }
    
      // *** Properties *** 
    
      public ContextMenu DropDown {
        get { return (ContextMenu)this.GetValue(DropDownProperty); }
        set { this.SetValue(DropDownProperty, value); }
      }
    
      // *** Overridden Methods *** 
    
      protected override void OnClick() {
        if (this.DropDown != null) {
          // If there is a drop-down assigned to this button, then position and display it 
    
          this.DropDown.PlacementTarget = this;
          this.DropDown.Placement = PlacementMode.Bottom;
    
          this.DropDown.IsOpen = true;
        }
      }
    }
    

    usage

    
      
        
          
          
          
        
      
    
    

    hope that helps you...

提交回复
热议问题