tags:

views:

92

answers:

4

Hi,

In my app, I'd like to make the context menu of a button appear when the user left clicks on the button. I can do this by creating an event handler for button's click event and then setting it's context menu's IsOpen property to true, but I'm wondering if there's a pure xaml solution to do the same thing. Any ideas?

Thanks!

A: 

I suppose you would have to use an event trigger on Button.Click. If it is then possible to use an element like

<Setter Property="ContextMenu.IsOpen" Value="True" />

you could be getting there.

flq
No... IsOpen is not a static property and it will not have any idea what context menu it is referring to.
Charlie
Huh? Does Button Have a ContextMenu property? and does a ContextMenu have an IsOpen property or not?
flq
A: 

You could make it appear by using an event trigger but you'd need another one to make it disappear. I can't think of any combination of existing properties that would get you anything useful though.

Jay
A: 

I would handle something like this through the view model class.

public Visibility ContextMenuVisibility
{
    get { return (this.ShowContextMenu) ? Visibility.Visible : Visibility.Collapsed; }
}

public bool ShowContextMenu { get; set; }

Handling those dynamic events that are halfway between the view and model is exactly what the view model layer is designed to do.

Oh, and I'm -1'ing myself for not being a pure XAML solution. Just offering up another alternative to a onCheckChanged event.

Jarrett Meyer
He never said he was using MVVM. And you can't downvote yourself but I'll do it for you, since this is not helping him at all. :)
Charlie
+1  A: 

You can create a new template for the button and then provide a context menu in the template's visual tree. Something like this:

<Style TargetType="{x:Type Button}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Button}">
        <Border>
          <ContentPresenter HorizontalAlignment="Center"
                            VerticalAlignment="Center"/>
          <Border.ContextMenu>
            <ContextMenu Name="contextMenu">
              <MenuItem Header="Here's a menu."/>
            </ContextMenu>
          </Border.ContextMenu>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

You could then trigger using the IsPressed property of the button and hook it up using a setter with contextMenu as the TargetName.

My real question is, what are you using this for? Opening a context-menu on left-click is going to be inherently flawed, because context-menus, by their nature, close when any other element is clicked. Which means this trigger, even when properly set up, will simply open and close the context menu immediately. I am curious as to why you need this kind of behavior; perhaps there is a better way of doing it than using a context-menu.

Charlie
I'm looking to build something similar to Office or IE where a button on the toolbar, when clicked, shows a menu underneath. In my scenario, each menu item in the menu is check-able so a user can quickly go and turn on/off things from the toolbar.
Steve the Plant
Ah. For that scenario, a context-menu is definitely not what you're looking for. What you should do is create your own unique type of button that has its own items collection. The items are then displayed in a drop-down style menu. Re-templating the combo-box control might be a good starting place.
Charlie