tags:

views:

32

answers:

1

I'm trying to have a WPF ViewBox 'appearing' at the cursor position in a user control when the user right-clicks on the control. Right now, I have the code:

<!-- XAML -->
<Viewbox Width="100" Visibility="Collapsed" x:Name="actionBox">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
        <Button>Item ▼</Button>
        <Button>Permute ▼</Button>
        <Button>Generate ▼</Button>
   </StackPanel>
</Viewbox>

and

/* C# */
private void setPanel_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    Point p = e.GetPosition(this);
    actionBox.Margin = new Thickness(p.X, p.Y, 0, 0);
    actionBox.Visibility = System.Windows.Visibility.Visible;
    actionBox.BringIntoView();
}

The event does get fired, but nothing seems to happen. (The MouseRightButtonDown="..." is in a different part of the XAML file.)

How would one go about writing this in WPF?

+2  A: 

Have a look at the Context Menu.

<ContextMenu Name="cm" StaysOpen="true">

                    <MenuItem Header="Item ▼"/>

                    <MenuItem Header="Permute ▼"/>

                    <MenuItem Header="Generate ▼"/>

</ContextMenu>

You can even bind the commands with the menu items as well create submenus.

Fore more information:

http://www.a2zdotnet.com/View.aspx?id=92

Archie
Thanks. I was looking for a slightly different UI but this seems to be the easiest way to do it!
Lucas Jones
you're welcome :)
Archie