WPF事件触发器添加BlurEffect

时间:2017-11-21 12:56:27

标签: c# wpf triggers styles

我想在某些情况下为我的窗口添加模糊效果,所以我写了一个自定义窗口样式。 - 我写了一个自定义样式,因为我在模糊效果前面有按钮,当应用模糊效果时,它会变得可见,但按钮不应该模糊。

我使用以下代码来应用模糊:

<AdornerDecorator.Effect>
  <BlurEffect Radius="{Binding Path=(local:StandardWindowEventHandler.LockedOverlayVisibility),
                       Converter={StaticResource VisibilityToBlurConverter}}"
              KernelType="Gaussian" />
</AdornerDecorator.Effect>

这很好但当我在TreeView中时,即使radius为0,GPU仍然是~50%。 没有模糊效果,它就像2%。现在我不想再设置半径,而是整个模糊效果。

我试过这个:

<AdornerDecorator.Triggers>
    <DataTrigger Binding="{Binding Path=(local:StandardWindowEventHandler.LockedOverlayVisibility)}"
                 Value="Visible">
            <Setter TargetName="PART_WindowAdornerDecorator"
                Property="Effect">
            <Setter.Value>
                <BlurEffect Radius="10" />
            </Setter.Value>
        </Setter>
    </DataTrigger>
</AdornerDecorator.Triggers>

不幸的是,触发器应该是事件触发器。如果我让事件触发器更改半径我什么都没赢,是否有可能通过事件触发器添加模糊效果?

提前致谢

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

<Grid x:Name="Root_Grid"
  VerticalAlignment="Stretch">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Click"
                    SourceName="Button1">
        <window:SetBlurEffectAction />
    </i:EventTrigger>
    <i:EventTrigger EventName="Click"
                    SourceName="Button2">
        <window:SetBlurEffectAction />
    </i:EventTrigger>
</i:Interaction.Triggers>
<AdornerDecorator ClipToBounds="True"
                  x:Name="PART_WindowAdornerDecorator">
    <ContentPresenter x:Name="PART_RootContentPresenter"
                      ContentTemplate="{TemplateBinding ActualWindowTemplate}"
                      dxr:RibbonControlHelper.IsAutoHide="{TemplateBinding RibbonAutoHideMode}"
                      dxr:RibbonControlHelper.DisplayShowModeSelector="{TemplateBinding DisplayShowModeSelector}" />
</AdornerDecorator>

您可以看到Root_Grid是AdornerDecorator的父级。 Button1和Button2是Root_Grid的一些孙子。

public class SetBlurEffectAction : TriggerAction<DependencyObject> {
    protected override void Invoke(object parameter) {
        var e = parameter as RoutedEventArgs;
        // OriginalSource is one of the buttons
        var parent = e?.OriginalSource as UIElement;
        // get Grid (parent of AdornerDecorator)
        while(true) {
            if(parent == null) {
                return;
            }
            var grid = parent as Grid;
            if("Root_Grid".Equals(grid?.Name)) {
                break;
            }
            parent = VisualTreeHelper.GetParent(parent) as UIElement;
        }
        // apply blur to this AdornerDecorator
        var deco = ((Grid)parent).GetElementByName("PART_WindowAdornerDecorator") as AdornerDecorator;
        if(deco == null) {
            return;
        }
        // != collapsed because the property is not updated yet
        if(StandardWindowEventHandler.LockedOverlayVisibility != Visibility.Collapsed) {
            deco.Effect = null;
        } else {
            deco.Effect = new BlurEffect {
                KernelType = KernelType.Gaussian,
                Radius = 7
            };
        }
    }
}

StandardWindowEventHandler.LockedOverlayVisibility是一个静态属性,在按下其中一个按钮时会更新。