Right-click on Listbox in Silverlight 4.

I am trying to implement what I used for adoption in Winforms applications. I am Silverlight noob, so I hope all this is elementary.

I have a list in a Silverlight 4 application. I would like to do the following:

  • Right click on the list
  • Enter the item below the place where I press the highlight button
  • I would like the context menu to pop up (with my own items in the context menu)

From my research, it still seems that there is no ContextMenu construct in Silverlight, instead we need to create a Grid / Canvas structure and attach it to a pop-up object, which then opens.

My questions are as follows:

  • To complete # 2, I need some sort of list entry test. I cannot figure out how to do this, and my google-fu is not helping.
  • Once I identify the index under the mouse, how do I really select the item?
  • Can I use a context menu component that can be reused? Extra credit if the component allows arbitrary submenus.
+3
source share
2 answers

I was looking for the same thing. I checked the Silverlight Control Toolkit in CodePlex and went through the samples (this is a very convenient resource), and here is what I found a solution for which you asked:

  • Create an ItemTemplate for your ListBox.

  • , " " ItemTemplate, ContextMenuService.ContextMenu, System.Windows.Controls.Input.Toolkit

  • MenuItem ContextMenu Click

  • , DataContext ( ListBox)

  • Selected, SelectedItem

, "Input- > ContextMenu" .

- , :

<ListBox ItemsSource="{StaticResource People}"
             Name="myListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}">
                    <controlsInputToolkit:ContextMenuService.ContextMenu>
                        <controlsInputToolkit:ContextMenu>
                            <controlsInputToolkit:MenuItem Header="Show in MessageBox"
                                                           Click="show_Click" />
                        </controlsInputToolkit:ContextMenu>
                    </controlsInputToolkit:ContextMenuService.ContextMenu>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

:

xmlns:controlsInputToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"

:

private void show_Click(object sender, RoutedEventArgs e)
    {
        var person = ((MenuItem)sender).DataContext as Person;
        if (null == person) return;
        MessageBox.Show("My Name is: " + person.Name);
        myListBox.SelectedItem = person;
    }

, :)

+3

MouseRightButtonDown. ListBox:

<ListBox Height="143" Name="listBox1" Width="218"
         MouseRightButtonDown="listBox1_MouseRightButtonDown" />

, . :

private void listBox1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
}

MouseButtonEventArgs GetPosition.

+1

Source: https://habr.com/ru/post/1751710/


All Articles