簡體   English   中英

Windows Phone 8-ListBoxItem的ListBox中的ContextMenu

[英]Windows Phone 8 - ContextMenu in ListBox for ListBoxItem

我有一個ListBox(綁定到一個可觀察的集合),並且在ItemsPanel StaticResource中定義了ListBoxItems設計。 ItemsPanel是在App.xml中定義的(就像將我的東西放在一個中央位置,而ListBoxItem的設計用於不同的頁面上)現在,對於每個列表框,我都需要具有不同的ContextMenu,然后事情就來了,我不要上班...

在我的App.xaml中(小型演示-不想在此處膨脹主題):

<Application.Resources>
    <DataTemplate x:Key="ListItemTemplate">
        <TextBlock Text="{Binding Title}" />
    </DataTemplate>
</Application.Resources>

在MainPage.xaml中,我定義了列表框:

<ListBox ItemsSource="{Binding Items}" 
         ItemTemplate="{StaticResource ListItemTemplate}">

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="toolkit:ContextMenuService.ContextMenu">
                <Setter.Value>
                    <toolkit:ContextMenu>
                        <toolkit:MenuItem Header="Test" />
                    </toolkit:ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

但是,如果我長按ListBoxItem,則什么也不會發生。 我沒有任何錯誤-沒什么-但是Contextmenu只是不顯示...(既不在模擬器中,也不在設備上)。

如果我將ContextMenu添加到LayoutRoot網格或其他內容,則它的工作原理就像一個超級按鈕。

您應該嘗試將ContextMenu放在ListItemTemplate中

<Application.Resources>
<DataTemplate x:Key="ListItemTemplate">
    <StackPanel>
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu>
                <toolkit:MenuItem x:Name="DoneMenuItem" 
                    Header="Done"
                    Command="{Binding Main.DoneCommand, Source={StaticResource Locator}}"
                    CommandParameter="{Binding}"/>
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <TextBlock Text="{Binding Title}" />
    </StackPanel>
</DataTemplate>

您可以刪除ListBox.ItemContainerStyle標記

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM