1

I want to solve a seemingly simple task. I want to create a list of text entries where each entry is selectable (and causes navigation to another page) and when the user holds his finger over an item I want a context menu with a single option to delete that item. This is very common pattern in WP applications. For example the browser does this with favourites.

Right now I have a listbox with a textblock in the item template and I start the navigation in the SelectionChanged event:

<ListBox Name="lbSnippets" SelectionChanged="lbSnippets_SelectionChanged">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"></TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>

I can think of several ways to solve the hold problem but none of them sits well with me. For example I may handle the Hold event on the TextBlock but then I will have to dig for the item related to this TextBlock. Something tells me that there should be a better way to do this as it is so common. What is the right way to solve this task?

4

1 に答える 1

2

WP7 用のSilverlight ツールキットには、コントロールが含まれていContextMenuます。

nuget を介してツールキットをインストールできます。PM> Install-Package SilverlightToolkitWP

次に、基本的に任意のコントロールに ContextMenus を追加します。

<DataTemplate>
    <TextBlock Text="{Binding}">
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu>
                <toolkit:MenuItem Header="Delete" 
                                  Command="{Binding YourDeleteCommand}"/>
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
    </TextBlock>
</DataTemplate>

xmltoolkit名前空間は次のとおりです。

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

この記事から ContextMenu コントロールについて学び始めることができます。

WP7 ContextMenu の詳細 | パート 1: 主要な概念と API

于 2012-08-17T13:46:50.653 に答える