[英]Can't apply dropShadow effect to border in ControlTemplate WPF
我已經在WPF中重寫了menu和menuItem ControlTemplate,並且將DropShadowEffect
添加到了菜單的Border(在Popup部分中),但是似乎不適用於我的菜單。
這是我的模板:
<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>
經過無數次嘗試后,我發現Popup
標簽阻止了DropShadowEffect
發射,我們必須采取另一種方式來實現我們的目標。
我已經通過使用另一個Border
模擬了DropShadowEffect
,然后在邊框中添加了一些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
添加。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.