首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF事件触发器添加BlurEffect

WPF事件触发器添加BlurEffect
EN

Stack Overflow用户
提问于 2017-11-21 12:56:27
回答 1查看 639关注 0票数 1

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

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

代码语言:javascript
复制
<AdornerDecorator.Effect>
  <BlurEffect Radius="{Binding Path=(local:StandardWindowEventHandler.LockedOverlayVisibility),
                       Converter={StaticResource VisibilityToBlurConverter}}"
              KernelType="Gaussian" />
</AdornerDecorator.Effect>

,这很好,但是当我在我的TreeView中的时候,GPU在50%左右,即使半径是0。无模糊效应,约为2%。现在我不想再设置半径,而是整个模糊效果。

我试过这样做:

代码语言:javascript
复制
<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>

不幸的是,触发器应该是事件触发器。如果我使事件触发器改变了我赢得的半径,是否有可能通过事件触发器添加模糊效果?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-21 16:12:04

我找到了一个解决办法:

代码语言:javascript
复制
<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的一些孙辈。

代码语言:javascript
复制
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是一个静态属性,当按下其中一个按钮时该属性将被更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47413686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档