在某些情况下,我想给我的窗口添加一个模糊效果,所以我写了一个自定义窗口样式。-I写了一个自定义样式,因为我在模糊效果前面有按钮,在应用模糊效果时会变得可见,但是这些按钮不应该变得模糊。
我使用了以下代码来应用模糊:
<AdornerDecorator.Effect>
<BlurEffect Radius="{Binding Path=(local:StandardWindowEventHandler.LockedOverlayVisibility),
Converter={StaticResource VisibilityToBlurConverter}}"
KernelType="Gaussian" />
</AdornerDecorator.Effect>,这很好,但是当我在我的TreeView中的时候,GPU在50%左右,即使半径是0。无模糊效应,约为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>不幸的是,触发器应该是事件触发器。如果我使事件触发器改变了我赢得的半径,是否有可能通过事件触发器添加模糊效果?
提前感谢
发布于 2017-11-21 16:12:04
我找到了一个解决办法:
<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是一个静态属性,当按下其中一个按钮时该属性将被更新。
https://stackoverflow.com/questions/47413686
复制相似问题