Windows Phone Toolkit:将ContextMenu添加到Button样式模板

时间:2013-09-19 14:13:41

标签: wpf xaml windows-phone-7 contextmenu silverlight-toolkit

我有一段XAML代码,用于为StackPanel中的某些按钮定义模板:

<StackPanel x:Name="ThumbnailsStack">
   <StackPanel.Resources>
      <Style TargetType="Button">
         <Setter Property="Height" Value="120" />
         <Setter Property="Margin" Value="3" />
         <Setter Property="BorderThickness" Value="0" />
      </Style>
    </StackPanel.Resources>
 </StackPanel>

代码有效,堆栈内的所有按钮都采用定义的样式。 现在我想将ContextMenu(来自Toolkit库)附加到它们中的每一个。我尝试了以下方式:

在App.xaml中

<Application.Resources>
    <toolkit:ContextMenu x:Key="ThumbBtnMenu">
        <toolkit:MenuItem Header="delete"></toolkit:MenuItem>
    </toolkit:ContextMenu>
</Application.Resources>

在上面的代码中,我添加了一个新标记:

<StackPanel x:Name="ThumbnailsStack">
   <StackPanel.Resources>
      <Style TargetType="Button">
         <Setter Property="Height" Value="120" />
         <Setter Property="Margin" Value="3" />
         <Setter Property="BorderThickness" Value="0" />
         <Setter Property="toolkit:ContextMenuService.ContextMenu" Value="{StaticResource ThumbBtnMenu}" />
      </Style>
    </StackPanel.Resources>
 </StackPanel>

现在,当页面加载时,它会引发System.Windows.Markup.XamlParseException: Failed to assign to property 'System.Windows.Setter.Value'

1 个答案:

答案 0 :(得分:1)

问题是当您在资源中定义上下文菜单时,将只有一个ContextMenu实例(并且同一个实例将添加到所有按钮),因此当它添加到可视树时会出现一些问题因为一个项目只能有一个父项。

我认为在Style级别定义Context菜单的唯一方法实际上是在按钮的完整stlye中定义它,例如:

<Style  TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
        <Setter Property="Padding" Value="10,5,10,6"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu>
                                <toolkit:MenuItem Header="delete"></toolkit:MenuItem>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>