English 中文(简体)
Right-click on a list Box in a银灯 4
原标题:Right-click on a Listbox in a Silverlight 4 app

我正试图执行我在Winforms申请中所说的话。 我是银星灯塔,因此,希望所有这一切都是最基本的。

我在银灯4中有一个名单箱。 我愿做以下工作:

  1. Right-click on the listbox
  2. Have the item under the location where I click highlight itself
  3. I d like a context menu to popup (with my own items in the context menu)

从我迄今为止的研究来看,似乎在银灯没有建筑背景材料,相反,我们必须建立Grid/Canvas结构,并将其附属于一个Popup物体,而后者当时正在流行。

我的问题如下:

  1. To accomplish #2, I need some kind of hit test on the listbox. I can t figure out how to do that and my google-fu isn t helping.
  2. Once I do identify the index under the mouse, how do I actually select the item?
  3. Is there a reusable Context menu component somewhere that I can use? Extra credit if the component allows arbitrary sub-menus.
最佳回答

我正在研究一下同样的问题。 在《刑法》中,我检查了Silverlight Control Toolkit,并通过样本(它拥有非常丰富的资源),我发现,这是你要求的解决办法:

  1. 为您编制的清单

  2. 在您希望成为项目Template的“直觉”的那部分,你将所附财产编号<编码>ContextMenuservice.ContextMenu存在于System.WindowsControls.Input.Toolkit之内。 名称:

  3. 在你的背景材料中添加MendItem控制,并将Click财产定在相应的点击事件手里

  4. 如果是手,则从投递人处获取数据内容(青年可以用来找到名单Box的相应内容)

  5. 作出这一规定 选择的,仅贴上<代码>,在名单箱内选定项目

  6. 3. 将任何习俗逻辑添加到活动手里

样本页上有一个例子,只是从导航线“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;
    }

我希望这有助于:

问题回答

http://msdn.microsoft.com/en-us/library/system.windows.uielement.mouserightbuttondown%28VS.9529.aspx” rel=“nofollow”>:。 如果在<代码>ListBox上对这一点具有约束力:

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

你们需要什么。 下面的法典是:

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

<代码>MouseButtonEventArgs将通过方法授予你职位。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签