【问题标题】:How to add icons to SecondaryCommands in Command Bar如何将图标添加到命令栏中的 SecondaryCommands
【发布时间】:2016-07-09 17:06:46
【问题描述】:

我正在尝试构建一个如下所示的 CommandBar:

具有具有图标的 SecondaryCommands 的所需 CommandBar

但我找不到一种方法来编辑 CommandOverflowPresenter 的默认样式,以便像在 Mail Windows 10 通用应用程序中一样显示 SecondaryCommands,左侧是图标,右侧是文本标签。

CommandOverflowPresenter 没有 Content 属性来围绕它构建控件模板并将内容元素绑定到 AppBarButton Icon 和 Label 属性。

我找到了这个article,但它没有显示 AppBarButton 的样式。

还有一个关于Stack Overflow的问题,结论是构建一个自定义的CommandOverflowPresenter样式。你是怎么做到的?

默认 CommandOverflowPresenter 样式:

<!-- Default style for Windows.UI.Xaml.Controls.CommandBarOverflowPresenter -->
<Style TargetType="CommandBarOverflowPresenter">
    <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundChromeMediumLowBrush}" />
    <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeHighBrush}" />
    <Setter Property="MaxWidth" Value="{ThemeResource CommandBarOverflowMaxWidth}"/>
    <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
    <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.ZoomMode" Value="Disabled" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CommandBarOverflowPresenter">
                <Grid x:Name="LayoutRoot" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"
                      BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,1" >
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="DisplayModeStates">
                            <VisualState x:Name="DisplayModeDefault" />
                            <VisualState x:Name="FullWidthOpenDown" >
                                <VisualState.Setters>
                                    <Setter Target="LayoutRoot.BorderThickness" Value="0,0,0,1" />
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="FullWidthOpenUp" >
                                <VisualState.Setters>
                                    <Setter Target="LayoutRoot.BorderThickness" Value="0,1,0,0" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ScrollViewer
                        HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                        HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                        VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                        VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                        ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}"
                        AutomationProperties.AccessibilityView="Raw">
                        <ItemsPresenter x:Name="ItemsPresenter" Margin="0,7,0,7"/>
                    </ScrollViewer>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是我尝试过的。

CommandOverflowPresenter 是一个 ItemsControl,我在 CommandOverflowPresenter 样式中为其 ItemTemplate 添加了一个 Setter,并让 Content 模板保持默认样式。

运行时,二级命令按照默认样式显示,只显示文本标签,不显示图标。

也许在 ItemTemplate 的 DataTemplate 中没有正确设置绑定?

<CommandBar.CommandBarOverflowPresenterStyle>
    <Style TargetType="CommandBarOverflowPresenter">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CommandBarOverflowPresenter">
                    <Grid x:Name="LayoutRoot" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"
                          BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,1" >

                        <!--<Grid.Resources>
                            <DataTemplate x:Key="SecondaryMenu">
                                <StackPanel Orientation="Horizontal">
                                    <SymbolIcon Symbol="{TemplateBinding AppBarButton.Icon}"/>
                                    <TextBlock Text="{TemplateBinding AppBarButton.Label}" />
                                </StackPanel>
                            </DataTemplate>
                        </Grid.Resources>-->

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="DisplayModeStates">
                                <VisualState x:Name="DisplayModeDefault" />
                                <VisualState x:Name="FullWidthOpenDown" >
                                    <VisualState.Setters>
                                        <Setter Target="LayoutRoot.BorderThickness" Value="0,0,0,1" />
                                    </VisualState.Setters>
                                </VisualState>
                                <VisualState x:Name="FullWidthOpenUp" >
                                    <VisualState.Setters>
                                        <Setter Target="LayoutRoot.BorderThickness" Value="0,1,0,0" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <ScrollViewer
                            HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                            HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                            VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                            VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                            ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}"
                            AutomationProperties.AccessibilityView="Raw">
                            <ItemsPresenter x:Name="ItemsPresenter" Margin="0,7,0,7">
                            </ItemsPresenter>
                        </ScrollViewer>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate x:DataType="AppBarButton">
                    <StackPanel Orientation="Horizontal">
                        <SymbolIcon Symbol="{Binding Icon}" />
                        <TextBlock Text="{Binding Label}" />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</CommandBar.CommandBarOverflowPresenterStyle>

我还尝试使用 SymbolIcon 和 Textblock 在 AppBarButton 标记内添加 StackPanel,但这也不起作用。

【问题讨论】:

    标签: xaml windows-10 uwp uwp-xaml


    【解决方案1】:

    我们可以编辑AppBarButton的默认样式和模板来实现这一点。而且我认为没有必要构建自定义CommandOverflowPresenter 样式。

    Microsoft 为默认模板提供了 XAML 代码,我们可以在 AppBarButton styles and templates 找到。我们可以把这个样式复制到Page.Resources,并给这个样式一个key,比如x:Key="MyAppBarButtonStyle"

    默认情况下,图标和文本垂直放置在“ContentRoot”StackPanel 中。我们可以像下面这样改变它以将它们水平放置。

    <StackPanel x:Name="ContentRoot" MinHeight="{ThemeResource AppBarThemeCompactHeight}" Orientation="Horizontal">
        <ContentPresenter x:Name="Content"
                          Margin="12,0,0,0"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Center"
                          AutomationProperties.AccessibilityView="Raw"
                          Content="{TemplateBinding Icon}"
                          Foreground="{TemplateBinding Foreground}" />
        <TextBlock x:Name="TextLabel"
                   Margin="12,0,12,0"
                   VerticalAlignment="Center"
                   FontFamily="{TemplateBinding FontFamily}"
                   FontSize="15"
                   Foreground="{TemplateBinding Foreground}"
                   Text="{TemplateBinding Label}"
                   TextAlignment="Center"
                   TextWrapping="Wrap" />
    </StackPanel>
    

    在此之后,我们需要像您提到的文章一样更改“溢出”VisualState,以确保图标可以显示在二级菜单中。

    <VisualState x:Name="Overflow">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentRoot" Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    

    除此之外,我们可能还需要更改AppBarButtonHorizontalAlignmentWidth 属性,如下所示,以便StackPanel 可以占据CommandOverflowPresenter 的全宽。

    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="Width" Value="Auto" />
    

    最后,我们可以在SecondaryCommands中使用这种风格了。

    <CommandBar.SecondaryCommands>
        <AppBarButton Style="{StaticResource MyAppBarButtonStyle}"
                      Click="AppBarButton_Click"
                      Icon="Like"
                      Label="Like" />
        <AppBarButton Style="{StaticResource MyAppBarButtonStyle}"
                      Click="AppBarButton_Click"
                      Icon="Dislike"
                      Label="Dislike" />
    </CommandBar.SecondaryCommands>
    

    完整的样式可能如下:

    <Style x:Key="MyAppBarButtonStyle" TargetType="AppBarButton">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Width" Value="Auto" />
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="AppBarButton">
                    <Grid x:Name="Root"
                          MinWidth="{TemplateBinding MinWidth}"
                          MaxWidth="{TemplateBinding MaxWidth}"
                          Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="ApplicationViewStates">
                                <VisualState x:Name="FullSize" />
                                <VisualState x:Name="Compact">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Overflow">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentRoot" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="OverflowWithToggleButtons">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentRoot" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Margin">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="38,0,12,0" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">
                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="OverflowTextLabel" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerUpThemeAnimation Storyboard.TargetName="OverflowTextLabel" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerDownThemeAnimation Storyboard.TargetName="OverflowTextLabel" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="InputModeStates">
                                <VisualState x:Name="InputModeDefault" />
                                <VisualState x:Name="TouchInputMode">
                                    <VisualState.Setters>
                                        <Setter Target="OverflowTextLabel.Padding" Value="0,11,0,13" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
    
                        <StackPanel x:Name="ContentRoot" MinHeight="{ThemeResource AppBarThemeCompactHeight}" Orientation="Horizontal">
                            <ContentPresenter x:Name="Content"
                                              Margin="12,0,0,0"
                                              HorizontalAlignment="Stretch"
                                              VerticalAlignment="Center"
                                              AutomationProperties.AccessibilityView="Raw"
                                              Content="{TemplateBinding Icon}"
                                              Foreground="{TemplateBinding Foreground}" />
                            <TextBlock x:Name="TextLabel"
                                       Margin="12,0,12,0"
                                       VerticalAlignment="Center"
                                       FontFamily="{TemplateBinding FontFamily}"
                                       FontSize="15"
                                       Foreground="{TemplateBinding Foreground}"
                                       Text="{TemplateBinding Label}"
                                       TextAlignment="Center"
                                       TextWrapping="Wrap" />
                        </StackPanel>
    
                        <TextBlock x:Name="OverflowTextLabel"
                                   Margin="12,0,12,0"
                                   HorizontalAlignment="Stretch"
                                   VerticalAlignment="Center"
                                   FontFamily="{TemplateBinding FontFamily}"
                                   FontSize="15"
                                   Foreground="{TemplateBinding Foreground}"
                                   Padding="0,5,0,7"
                                   Text="{TemplateBinding Label}"
                                   TextAlignment="Left"
                                   TextTrimming="Clip"
                                   TextWrapping="NoWrap"
                                   Visibility="Collapsed" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 2011-09-18
      • 2017-12-16
      • 1970-01-01
      • 2013-07-21
      • 2021-01-12
      相关资源
      最近更新 更多