C# WPF: Как установить задний фон и изображение в стиле для MenuItem?
У меня есть стиль для ContextMenu, там я задал только задний фон и убрал обводку:
<Style x:Key="TrayIconStyle" TargetType="ContextMenu">
<Setter Property="Background" Value="#222222"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
Для объекта MenuItem этого меню я тоже сделал стиль. По моей задумке он должен устанавливать иконку и текст в StackPanel и немного смещать текст при наведении мыши (IsMouseOver):
<Style TargetType="MenuItem">
<Setter Property="Background" Value="#222222"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MenuItem">
<StackPanel Orientation="Horizontal">
<Image x:Name="_imgicon" Stretch="Fill" Source="{Binding Icon,RelativeSource={RelativeSource TemplatedParent}}" />
<TextBlock Foreground="Gray" FontSize="14" Margin="5 0 0 0" VerticalAlignment="Center" x:Name="_txt" Text="{Binding Header,RelativeSource={RelativeSource TemplatedParent}}" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="_txt" Property="Foreground" Value="White"/>
<Setter TargetName="_txt" Property="Margin" Value="8 0 0 0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Однако все, что работает - это текст и его "анимация" смещения при наведении мыши. Иконка почему-то не отображается и имеется какая-то белая полоса слева, к которой я бы тоже хотел применить цвет фона #222222:
В чем моя ошибка?
Создание контекстного меню:
<ContextMenu Style="{StaticResource TrayIconStyle}">
<MenuItem Header="Open">
<MenuItem.Icon>
<Image Source="/ImgSrc/logo.ico" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Exit"/>
</ContextMenu>
Ответы (1 шт):
На базе прошлого вашего вопроса делал пример для иконки системного трея, но потом вы удалили вопрос.
Разметка меню выглядит так.
<ContextMenu>
<ContextMenu.Resources>
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border BorderThickness="1" x:Name="Border" BorderBrush="{StaticResource {x:Static SystemColors.HighlightBrushKey}}" Padding="1" Background="#0f3c5a">
<StackPanel ClipToBounds="True" Orientation="Vertical" IsItemsHost="True" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ContextMenu.Resources>
<MenuItem Header="Show Window" Command="{Binding ShowWindowCommand}">
<MenuItem.Icon>
<Image Source="Circle32x32.ico" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Hide Window" Command="{Binding HideWindowCommand}" />
<Separator Background="{StaticResource {x:Static SystemColors.HighlightBrushKey}}" Margin="1" />
<MenuItem Header="Exit" Command="{Binding CloseWindowCommand}" />
</ContextMenu>
Решение целиком на Яндекс.Диске - https://disk.yandex.ru/d/tIydIVE0PVg8wQ

