WPF:如何在有条件的情况下将图像/图标添加到我的菜单

时间:2018-07-08 19:18:22

标签: wpf menu menuitem

这是我的Style

<Style TargetType="{x:Type Menu}" x:Key="StandardMenu">
        <Style.Resources>
            <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="Separator">
                <Setter Property="Height" Value="1"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Separator">
                            <Border BorderBrush="{StaticResource MenuSeparatorBorderBrush}" BorderThickness="1" Margin="25,0,0,0"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style TargetType="{x:Type MenuItem}">
                <Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Menu}}}"/>
                <Setter Property="FontSize" Value="{DynamicResource ApplicationFontSize}"/>
                <Setter Property="Command" Value="{Binding Command}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type MenuItem}">
                            <!--Border 1-->
                            <Border x:Name="Border" Background="Transparent" BorderBrush="Transparent"  CornerRadius="2" BorderThickness="1" SnapsToDevicePixels="False">
                                <Grid x:Name="Grid">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition x:Name="Col0" MinWidth="17" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup"/>
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="MenuTextColumnGroup"/>
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/>
                                        <ColumnDefinition x:Name="Col3" Width="14"/>
                                    </Grid.ColumnDefinitions>
                                    <ContentPresenter Grid.Column="0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/>
                                    <ContentPresenter Grid.Column="1" Margin="{TemplateBinding Padding}" x:Name="HeaderHost" RecognizesAccessKey="True" ContentSource="Header" VerticalAlignment="Center"/>
                                    <ContentPresenter Grid.Column="2" Margin="8,1,8,1" x:Name="IGTHost" ContentSource="InputGestureText" VerticalAlignment="Center"/>
                                    <Grid Grid.Column="3" Margin="4,0,6,0" x:Name="ArrowPanel" VerticalAlignment="Center">
                                        <Path x:Name="ArrowPanelPath" HorizontalAlignment="Right" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,0 L0,8 L4,4 z"/>
                                    </Grid>
                                    <Popup IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" 
                                           Placement="Right"
                                           HorizontalOffset="-1" 
                                           x:Name="SubMenuPopup"
                                           Focusable="false"
                                           PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
                                           AllowsTransparency="True">
                                        <Grid Margin="0,0,5,5">
                                            <!--Border 2-->
                                            <Border x:Name="SubMenuBorder" CornerRadius="5"
                                                    BorderBrush="{StaticResource MenuSeparatorBorderBrush}"
                                                    BorderThickness="1" 
                                                    Background="{StaticResource SubmenuItemBackground}" 
                                                    SnapsToDevicePixels="True">
                                                <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True" Margin="2">
                                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
                                                </Grid>
                                                <Border.Effect>
                                                    <DropShadowEffect ShadowDepth="2" Color="Black"/>
                                                </Border.Effect>
                                            </Border>
                                            <!--Border 3-->
                                            <Border Margin="1,0,0,0"
                                                    x:Name="TransitionBorder"
                                                    Width="0" 
                                                    Height="2" 
                                                    VerticalAlignment="Top"
                                                    HorizontalAlignment="Left" 
                                                    Background="{StaticResource SubmenuItemBackground}"
                                                    SnapsToDevicePixels="False"
                                                    BorderThickness="1" 
                                                    BorderBrush="{StaticResource SubmenuItemBackground}"/>
                                        </Grid>
                                    </Popup>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsSelected}" Value="true">
                                    // Here i want to see my icon/image
                                </DataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
        <Setter Property="Background" Value="{StaticResource LightBackground}"/>
        <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
    </Style>

我的View model拥有名为Property的{​​{1}},并且当此IsSelected值为true时,我想从我的bool文件夹中查看图像。

所以我知道我的触发器应该是类似的东西:

Resources

但是我需要在此图像中添加我的 <DataTrigger Binding="{Binding IsSelected}" Value="true"> // Here i want to see my icon/image </DataTrigger> 吗?

更新

这是我的Style

Menu

结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

您还没有指定图像的目标位置,因此我假设它应该位于网格的最左列。您可以通过对图像进行样式设置并向其中添加DataTrigger来实现此目的:

<Style x:Key="MenuImageStyle" TargetType="{x:Type Image}">
    <Setter  Property="Source" Value="check.png" />     <!--Image filename-->
    <Setter  Property="Visibility" Value="Hidden" />    <!--Default value-->
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsSelected}" Value="true">
            <Setter  Property="Visibility" Value="Visible" />
        </DataTrigger>
    </Style.Triggers>
</Style>

然后将图像添加到网格元素:

<Image Grid.Column="0" Style="{StaticResource MenuImageStyle}" />