.net 模糊 WPF 容器的背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7815278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:58:14  来源:igfitidea点击:

Blur the background of the WPF container

.netwpfeffectsblur

提问by Maxim V. Pavlov

Ultimately, what I want to achieve is a replication to some extend, of an Aero glass functionality of a WPF content control.

最终,我想要实现的是在某种程度上复制 WPF 内容控件的 Aero glass 功能。

If I apply the BlurEffectto a StackPanelthat contains a TextBlock, I will have the TextBlock's text blurred.

如果我申请了BlurEffect一个StackPanel包含TextBlock,我将拥有TextBlock的文字模糊不清。

Consider an examples:

考虑一个例子:

No blur

没有模糊

no blur

没有模糊

and with <BlurEffect Radius="5" KernelType="Gaussian"/>

<BlurEffect Radius="5" KernelType="Gaussian"/>

with blur

模糊

But is there a WPF way to blur the background behind the panel, and not it's contents?

但是有没有一种 WPF 方法来模糊面板后面的背景,而不是它的内容?

The background of the StackPanelis a desktop, and the window that hosts it is set to AllowTransparency="True"to allow the custom-shaped look.

的背景StackPanel是桌面,承载它的窗口设置AllowTransparency="True"为允许自定义形状的外观。

采纳答案by WPF-it

What the background of the StackPanel? an ImageBrush? If so why cant you apply BlurEffect to that?

StackPanel 的背景是什么?ImageBrush?如果是这样,为什么不能将 BlurEffect 应用到它?

If that is not possible then try this..

如果这是不可能的,那么试试这个..

1] Use an image and draw it completely over Gridas I see you dont want a TileEffect. Add BlurEffectto this Image. Make sure you fill image the uniformly.

1] 使用图像并将其完全绘制,Grid因为我发现您不想要TileEffect. 添加BlurEffect到此Image。确保均匀地填充图像。

2] Then add StackPanelwith transparent background as next childin the Gridi.e. dont reverse the order of image and stackpanel.

2]然后添加StackPanel透明背景作为下一个孩子Grid即不要颠倒图像和堆栈面板的顺序。

3] Then add TextBlockin StackPanel.

3]然后TextBlock在StackPanel中添加。

OR

或者

If you insist on using a Brushto be set as the backgrounnd of the panel then use VisualBrushthat draws a blurred image as background of the stackpanel, instead of ImageBrush.

如果您坚持使用 aBrush设置为面板的背景,则使用VisualBrush它绘制模糊图像作为堆栈面板的背景,而不是ImageBrush.

Let me know if any of these tips help.

如果这些提示有帮助,请告诉我。

回答by Patrick Klug

no, it is not possible. The Effect is applied to the element and all its childrenbut you can easily place the TextBlock outside the container, rather than inside it.

不,这是不可能的。Effect 应用于元素及其所有子元素,但您可以轻松地将 TextBlock 放在容器外,而不是放在容器内。

Normally you would use a grid like so:

通常你会像这样使用网格:

<Grid>
  <Border>
     <Border.Effect>
      <BlurEffect Radius="5" KernelType="Gaussian"/>
     </Border.Effect/>
  </Border>
  <TextBlock .../>
</Grid>

In your example that will make no difference though. What, exactly, are you trying to blur?

在您的示例中,这没有任何区别。你到底想模糊什么?

回答by J?rg Flechsig

You can use the SetWindowCompositionAttributeon a Systems.Window, but you are then forced to set the WindowStyle to "None" and implement your own native Window funtionality and handles. Also inheriting from a custom control is quite complicated. Long story short, there's BlurryControls.

您可以在 Systems.Window 上使用SetWindowCompositionAttribute,但随后您必须将 WindowStyle 设置为“None”并实现您自己的本机 Window 功能和句柄。从自定义控件继承也是相当复杂的。长话短说,有 BlurryControls。

You can find it via NuGetby browsing for "BlurryControls" or check out the code yourself on GitHub. Eitherway, I hope this is helpful. It uses .NET 4.5.2 and only works for Windows10, since there is no solution to this problem on Windows8, and in earlier versions (Windows7 and Vista) you can achieve this by accessing DwmEnableBlurBehindWindow.

你可以通过NuGet通过浏览“BlurryControls”找到它,或者在GitHub 上自己查看代码。不管怎样,我希望这会有所帮助。它使用 .NET 4.5.2 并且仅适用于 Windows10,因为在 Windows8 上没有解决此问题的方法,而在早期版本(Windows7 和 Vista)中,您可以通过访问DwmEnableBlurBehindWindow来实现此目的。

On GitHub you will also find a sample application called BlurryWindowInvoker.

在 GitHub 上,您还会找到一个名为 BlurryWindowInvoker 的示例应用程序。

回答by Ali Al-Masry

You could apply this to a Grid.

您可以将其应用于网格。

<Grid.Background>
   <VisualBrush>
       <VisualBrush.Visual>
          <Image Source="RESOURCES/BACKGROUNDS/BACKGROUND_01.jpg">
             <Image.BitmapEffect>
                <BlurBitmapEffect KernelType="Gaussian" Radius="20" />
             </Image.BitmapEffect>
          </Image>
       </VisualBrush.Visual>
    </VisualBrush>
 </Grid.Background>