代码之家  ›  专栏  ›  技术社区  ›  TheUnexpected

Windows Phone工具包:将ContextMenu添加到按钮样式模板

  •  1
  • TheUnexpected  · 技术社区  · 11 年前

    我有一段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 回复  |  直到 11 年前
        1
  •  1
  •   Benoit Catherinet    11 年前

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

    我认为在样式级别定义上下文菜单的唯一方法实际上是在按钮的完整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>