Windows phone 7 按下时如何在列表框项目周围放置边框

Windows phone 7 按下时如何在列表框项目周围放置边框,windows-phone-7,xaml,listbox,windows-phone-8,Windows Phone 7,Xaml,Listbox,Windows Phone 8,我有一个自定义样式,用于在列表框中的选定项周围放置边框。但是,我在这个列表框中放置了一个ContextMenu,当按下item以获取上下文菜单时,它不会在选定的列表框项目周围放置边框。似乎只有当用户按下该项然后释放时,ListBox项才有边框。当用户想要访问ContextMenu时,以及当用户执行正常的选择项操作时,如何在这两种情况下的ListBox项周围放置边框 手机应用程序页面资源中的样式 <phone:PhoneApplicationPage.Resources> &l

我有一个自定义样式,用于在列表框中的选定项周围放置边框。但是,我在这个列表框中放置了一个ContextMenu,当按下item以获取上下文菜单时,它不会在选定的列表框项目周围放置边框。似乎只有当用户按下该项然后释放时,ListBox项才有边框。当用户想要访问ContextMenu时,以及当用户执行正常的选择项操作时,如何在这两种情况下的ListBox项周围放置边框

手机应用程序页面资源中的样式

<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>
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu x:Name="imgListContextMenu" Background="{StaticResource PhoneChromeBrush}">
                                <toolkit:MenuItem Foreground="{StaticResource PhoneForegroundBrush}" Header="edit" Click="editContextMenuItem_Click"/>
                                <toolkit:MenuItem Foreground="{StaticResource PhoneForegroundBrush}" Header="favorite" Click="favoriteContextMenuItem_Click"/>
                                <toolkit:MenuItem Foreground="{StaticResource PhoneForegroundBrush}" Header="delete" Click="deleteContextMenuItem_Click"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

我的页面网格中的列表框

<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>

使用
鼠标悬停
状态

<VisualState x:Name="MouseOver">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="brd">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>


我将其放置在VisualState上方的鼠标中,但它会无缘无故破坏调试器。这是因为您没有
ContentBorder
属性,或者在简单语言中,您的边框名称是
brd
,而在我的示例中,它是
ContentBorder
。不管怎样,编辑我的答案。哦,对了,太棒了。但由于某些原因,它仍然不起作用。我还引用并测试了
FocusStates
VisualStatesGroup,但它也没有任何效果。我已经在代码中测试了这一点,它正在工作。我正在为windows phone 8进行开发,并在诺基亚620设备上进行了测试。嗯,是的,我正在使用WP8以及诺基亚920进行测试。当然,当您按住ListBoxItem以访问ContextMenu时,会出现边框吗?对我来说,ListBoxItem周围的边框仅在用户选择一个项目,然后从屏幕上松开手指时才会出现。