0

我试图在我的 ListBox 的 DataTemplate 中放置一个 ContextMenu。ListBox 放置在 PivotItem 内。由于某种原因,ContextMenu 没有显示在 ListBox 中的项目被按下。我不确定该错误,因为错误列表中或调试时没有显示任何内容。

主页.xaml

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="MyStyle" TargetType="ListBoxItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property ="Foreground" Value="White" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" Background="{TemplateBinding Background}" 
            HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
            VerticalAlignment="{TemplateBinding VerticalAlignment}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            BorderThickness="{TemplateBinding BorderThickness}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Opacity" Duration="0" To=".5" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="brd"
                                Storyboard.TargetProperty="BorderThickness">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="2" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="brd" CornerRadius="10" BorderBrush="{StaticResource PhoneAccentBrush}" Width="Auto" BorderThickness="{TemplateBinding BorderThickness}">
                            <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>                                
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

<phone:PivotItem Header="recent">
            <ListBox x:Name="Recent" ItemsSource="{Binding Pictures}" Margin="8" 
                     SelectionChanged="recent_SelectionChanged" toolkit:TiltEffect.IsTiltEnabled="True"
                     ItemContainerStyle="{StaticResource MyStyle}">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu x:Name="imgListContextMenu">
                                <toolkit:MenuItem Header="share" Tap="shareContextMenuItem_Tap"/>
                                <toolkit:MenuItem Header="favorite" Tap="favoriteContextMenuItem_Tap"/>
                                <toolkit:MenuItem Header="set as start screen" Tap="setAsStartScreenContextMenuItem_Tap"/>
                                <toolkit:MenuItem Header="delete" Tap="deleteContextMenuItem_Tap"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
         </phone:PivotItem>

此外,Tap 是在每个 ContextMenu 项中使用的最佳事件处理程序方法吗?

4

2 回答 2

0

为什么不使用

 <ListBox.ContextMenu>
    <ContextMenu>...
 </ListBox.ContextMenu>
于 2013-06-17T07:16:03.303 回答
0

因为我的 ListBox 有自己的自定义样式,所以我将上下文菜单放在了图像绑定发生的位置。

在 VisualStateManager 的结束标记下..

</VisualStateManager.VisualStateGroups>
                        <Border x:Name="brd" CornerRadius="10" BorderBrush="{StaticResource PhoneAccentBrush}" Width="Auto" BorderThickness="{TemplateBinding BorderThickness}">
                            <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>                                
                        </Border>
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu x:Name="imgListContextMenu">
                                ...
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
于 2013-06-23T13:05:42.930 回答