无法对ControlTemplate WPF中的边框应用dropShadow效果

时间:2018-07-13 04:55:07

标签: c# wpf menuitem controltemplate

我在WPF中重写了menu和menuItem ControlTemplate,并在菜单的Border(在Popup部分中)添加了DropShadowEffect,但似乎不适用于我的菜单。

这是我的模板:

 <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="MenuItem">
     <Border Name="Border" Background="White">
         <Grid VerticalAlignment="Center">
             <StackPanel Orientation="Horizontal">
                 <ContentPresenter  Margin="6,3,3,3" VerticalAlignment="Center" ContentSource="Header" RecognizesAccessKey="True" />
                 <TextBlock Margin="0 0 3 0" HorizontalAlignment="Center" VerticalAlignment="Center" Text="▾" FontSize="13"/>
             </StackPanel>
             <Popup Name="Popup" Placement="Bottom" VerticalOffset="3" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
                 <Border Name="SubmenuBorder" SnapsToDevicePixels="True" Background="White" BorderBrush="Red"  BorderThickness="5" >
                     <Border.Effect>
                         <DropShadowEffect ShadowDepth="0" Color="#000" Opacity="1" BlurRadius="10"/>
                     </Border.Effect>
                     <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                 </Border>
             </Popup>
         </Grid>
     </Border>
     <ControlTemplate.Triggers>
         <Trigger Property="IsSuspendingPopupAnimation" Value="true">
             <Setter TargetName="Popup" Property="PopupAnimation" Value="None"/>
         </Trigger>
         <Trigger Property="IsHighlighted" Value="true">
             <Setter TargetName="Border" Property="Background" Value="{StaticResource ItemHighlight}"/>
             <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
         </Trigger>
         <Trigger Property="IsEnabled" Value="False">
             <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
         </Trigger>
         <Trigger Property="IsEnabled" Value="True">
             <Setter Property="Foreground" Value="{StaticResource EnabledForegroundBrush}"/>
         </Trigger>
         <Trigger Property="IsPressed" Value="true">
             <Setter TargetName="Border" Property="Background" Value="{StaticResource MenuClick}"/>
         </Trigger>
         <Trigger Property="IsSubmenuOpen" Value="true">
             <Setter TargetName="Border" Property="Background" Value="{StaticResource MenuClick}"/>
         </Trigger>
     </ControlTemplate.Triggers>
 </ControlTemplate>

1 个答案:

答案 0 :(得分:0)

经过多次尝试,我发现Popup标签阻止了DropShadowEffect的发射,我们必须采取另一种方式来实现我们的目标。

我通过使用另一个DropShadowEffect模拟了Border,然后向边界添加了Padding:我已经将DropShadowEffect添加到InnerBorder(我旧的Border),效果很好:)

这是我编辑过的部分:

 <Popup Name="Popup" Placement="Bottom" VerticalOffset="2" HorizontalOffset="-8" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
     <Border Padding="8 0 8 8">
         <Border Name="SubmenuBorder" SnapsToDevicePixels="True" Background="{StaticResource MenuClick}" BorderBrush="{StaticResource MenuClick}"  BorderThickness="5">
             <Border.Effect>
                 <DropShadowEffect ShadowDepth="0" Color="#AAA" Opacity="1" BlurRadius="10"/>
             </Border.Effect>
             <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
     </Border>
     </Border>
 </Popup>                                   

我还向HorizentalOffset添加了Popup,以校准Padding的添加。