問題描述
我正在嘗試實現(xiàn)我過去在 Winforms 應用程序中認為理所當然的內(nèi)容.我是 Silverlight 菜鳥,所以希望所有這些都是初級的.
I am trying to implement what I used to take for granted in Winforms applications. I am a Silverlight noob, so hopefully all this is elementary.
我在 Silverlight 4 應用中有一個列表框.我想執(zhí)行以下操作:
I have a listbox in a Silverlight 4 app. I'd like to do the following:
- 右鍵單擊列表框
- 將項目放在我點擊突出顯示的位置
- 我想要彈出一個上下文菜單(在上下文菜單中有我自己的項目)
從我目前的研究來看,Silverlight 中似乎沒有 ContextMenu 構(gòu)造,相反,我們必須構(gòu)建一個 Grid/Canvas 結(jié)構(gòu)并將其附加到一個 Popup 對象,然后彈出該對象.
From my research so far, it appears that there is no ContextMenu construct in Silverlight, instead we have to build up a Grid/Canvas structure and attach it to a Popup object, which is what is then popped up.
我的問題如下:
- 要完成 #2,我需要對列表框進行某種命中測試.我不知道該怎么做,我的 google-fu 也沒有幫助.
- 一旦我確定了鼠標下的索引,我該如何實際選擇項目?
- 是否有我可以使用的可重用上下文菜單組件?如果組件允許任意子菜單,則額外加分.
推薦答案
我一直在尋找同樣的事情.我在 CodePlex 檢查了 Silverlight Control Toolkit 并瀏覽了示例(這是一個非常方便的資源),這就是我發(fā)現(xiàn)是您所問問題的解決方案:
I've been looking around for the same thing. I checked the Silverlight Control Toolkit at CodePlex and went through the samples (it's a very handy resource) and here's what I found to be the solution to what you asked:
為您的列表框創(chuàng)建一個 ItemTemplate
Create an ItemTemplate for your ListBox
在您希望成為可右鍵單擊"的 ItemTemplate 的部分中,設置 System.Windows.Controls.Input 中存在的附加屬性
命名空間ContextMenuService.ContextMenu
.Toolkit
in the part that you want to be "right-clickable" of your ItemTemplate set the attached property ContextMenuService.ContextMenu
that exists within the System.Windows.Controls.Input.Toolkit
namespace
將 MenuItem 控件添加到您的 ContextMenu 并將 Click 屬性設置為相應的點擊事件處理程序
add MenuItem controls to your ContextMenu and set the Click property to the corresponding click event handler
在事件處理程序中,從發(fā)送方獲取DataContext(您可以使用它在ListBox中找到相應的元素)
in the event handler, get the DataContext from the sender (you can use that to find the corresponding element in the ListBox)
要使該元素被選中,只需將列表框中的 SelectedItem
屬性設置為它
to make that element Selected, just set the SelectedItem
property in the list box to it
向事件處理程序添加任何自定義邏輯
Add any custom logic to the event handler
示例頁面中有一個示例,只需從導航窗格中轉(zhuǎn)到Input->ContextMenu"即可.
There's an example in the samples page, just go to "Input->ContextMenu" from the navigation pane.
如果你想要簡潔的東西,這是一個簡化的例子:
If you want something concise, Here's a simplified example:
<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;
}
我希望這會有所幫助:)
I hope this helps :)
這篇關(guān)于右鍵單擊 Silverlight 4 應用程序中的列表框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!