stigzler
stigzler

Reputation: 993

WPF - Change Forecolor of sub-MenuItem

I'm afraid WPF Templates baffle me. I was needing a control similar to Winforms' DropDownButton. I'm nearly there by using a MenuItem Control, but I can't figure how to change the forecolor of the sub-menu items. Here's my code and the result:

<Style x:Key="DropDownButton" TargetType="{x:Type MenuItem}">
    <Setter Property="Background" Value="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}"/>
    <Setter Property="Width" Value="22" />
    <Setter Property="Height" Value="22" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MenuItem}">                    
                <Grid SnapsToDevicePixels="true" Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}">
                    <Border x:Name="Bd" BorderBrush="Transparent" BorderThickness="1">
                        <DockPanel>
                            <ContentPresenter x:Name="Icon" Width="16" Height="16" Margin="2" ContentSource="Icon" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <Path x:Name="GlyphPanel" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="7,0,0,0" Visibility="Collapsed" VerticalAlignment="Center" />
                            <ContentPresenter x:Name="content" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </DockPanel>
                    </Border>
                    <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" HorizontalOffset="1" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom" VerticalOffset="-1">
                        <Border BorderThickness="1" BorderBrush="Gray" Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}">
                            <ScrollViewer x:Name="SubMenuScrollViewer"  CanContentScroll="true" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                                <Grid RenderOptions.ClearTypeHint="Enabled">
                                    <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle" />
                                </Grid>
                            </ScrollViewer>
                        </Border>
                    </Popup>
                </Grid>                    
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="True">
                        <Setter Property="Background" TargetName="Bd" Value="#2000A2Ed"/>                            
                        <Setter Property="BorderBrush" TargetName="Bd" Value="Gray"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<!-- ... -->
<Menu d:Background="Transparent" ToolTip="Sort Gists">
    <MenuItem Style="{StaticResource DropDownButton}">
        <MenuItem.Icon>
            <imaging:CrispImage Moniker="{x:Static catalog:KnownMonikers.SortAscending}" />
        </MenuItem.Icon>
        <MenuItem Header="Alphabetical" Command="{Binding SortGistsCMD}" CommandParameter="{x:Static enums:GistSortMethod.Alphabetical}">
            <MenuItem.Icon>
                <imaging:CrispImage Moniker="{x:Static catalog:KnownMonikers.SortAscending}" />
            </MenuItem.Icon>
        </MenuItem>
        <MenuItem Header="Starred" Command="{Binding SortGistsCMD}" CommandParameter="{x:Static enums:GistSortMethod.Starred}">
            <MenuItem.Icon>
                <imaging:CrispImage Moniker="{x:Static catalog:KnownMonikers.Favorite}" />
            </MenuItem.Icon>
        </MenuItem>
        <MenuItem Header="Private" Command="{Binding SortGistsCMD}" CommandParameter="{x:Static enums:GistSortMethod.Public}">
            <MenuItem.Icon>
                <imaging:CrispImage Moniker="{x:Static catalog:KnownMonikers.Key}" />
            </MenuItem.Icon>
        </MenuItem>
    </MenuItem>
</Menu>

Result:

enter image description here

How do I set the text color? Value="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}" resolves to white in VS DarkMode, but this displays as black. I know this is likely setting the 'button' text color rather than the dropdownitems.

Upvotes: 1

Views: 43

Answers (0)

Related Questions