DropShadowEffect在WPF自定义控件上多次应用

时间:2016-02-16 09:25:27

标签: c# .net wpf custom-controls

我正在编写一个WPF自定义控件,用于显示加载进程的叠加层。此自定义控件派生自ContentControl并重用其Effect依赖项属性以显示投影。

然而,投影无意中显示并且不希望地显示两次。我试图找到任何逻辑,但不知道如何解决这个问题。任何人都知道如何去除外边框上的阴影?

Shadow displayed on outer border and inner border.

LoadingOverlay.cs

public class LoadingOverlay : ContentControl
{
    static LoadingOverlay()
    {
        Type currentType = typeof(LoadingOverlay);
        FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
            currentType,
            new FrameworkPropertyMetadata(currentType));
    }
}

主题\ Generic.xaml

<Style TargetType="local:LoadingOverlay">
    <Setter Property="Background" Value="#BBFFFFFF" />
    <Setter Property="BorderBrush" Value="#FF266ECC" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect ShadowDepth="0" />
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:LoadingOverlay">
                <Border Background="{TemplateBinding Background}" Effect="{x:Null}">
                    <Border Effect="{TemplateBinding Effect}" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FFFFFFFF" BorderBrush="#FF266ECC" BorderThickness="1">
                        <ContentPresenter VerticalAlignment="Center" Margin="15" RecognizesAccessKey="True" />
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

尝试在内部边框级别设置效果,而不是在LoadingOverlay级别设置效果。然后你不必将上边框效果设置为{x:null}。见下文:

<Style TargetType="local:LoadingOverlay">
<Setter Property="Background" Value="#BBFFFFFF" />
<Setter Property="BorderBrush" Value="#FF266ECC" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="local:LoadingOverlay">
            <Border Background="{TemplateBinding Background}">
                <Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FFFFFFFF" BorderBrush="#FF266ECC" BorderThickness="1">
                    <ContentPresenter VerticalAlignment="Center" Margin="15" RecognizesAccessKey="True" />
                    <Border.Effect>
                        <DropShadowEffect ShadowDepth="0" />
                    </Border.Effect>
                </Border>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>