Windows Phone上下文菜单

时间:2013-02-19 18:41:21

标签: windows-phone-7 xaml contextmenu

我想将上下文菜单添加到列表框中。但是,当我按住列表框项目时,没有任何反应。  感谢..

此代码是我的列表框的定义。我将上下文菜单添加到列表框中。

<ListBox Grid.Row="1" Name="chList" ItemsSource="{Binding Ch.Texts}" SelectionChanged="TextChanged" Style="{StaticResource ListOfText}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu Name="ContextMenu">
                    <toolkit:MenuItem Name="Edit" Header="Edit" Click="Edit_Click"/>
                    <toolkit:MenuItem Name="Delete" Header="Delete" Click="Delete_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
        </DataTemplate>
    </ListBox.ItemTemplate>
</Listbox>

in styles.xaml

<Style x:Key="ListOfText" TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border x:Name="RootElement" Padding="{StaticResource PhoneBorderThickness}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="10" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0" Background="{StaticResource PhoneAccentBrush}"
                                    Opacity="{Binding IsRead,Converter={StaticResource opacityConverter}}" />
                        <Grid MinHeight="60" Grid.Column="1">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="{Binding Title}" TextWrapping="Wrap"
                                           Style="{StaticResource PhoneTextTitle3Style}" />
                            <Grid Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{Binding PbDate, Converter={StaticResource dateConverter}}"
                                               VerticalAlignment="Center" TextWrapping="Wrap" Style="{StaticResource PhoneTextSmallStyle}" />
                                <Image Grid.Column="1" Height="{StaticResource PhoneFontSizeNormal}" HorizontalAlignment="Left"
                                           Visibility="{Binding IsStared,Converter={StaticResource visibilityConverter}}" Source="/Toolkit.Content/favs.png" />
                            </Grid>
                        </Grid>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在mysolution中的Styles.xaml ......但我无法解决这个问题..

...谢谢

1 个答案:

答案 0 :(得分:0)

您要将项DataTemplate设置为ContextMenuService.ContextMenu,并且没有实际内容。你需要实际显示一些内容。此外,列表中是否有任何项目?

ContextMenuService.ContextMenu移到一个主模板中 - 目前您正在拆分它(原因不明)。删除DataTemplate控件本身中的ListBox声明,并使用您预先定义的样式。