【发布时间】:2015-07-23 16:52:37
【问题描述】:
VisualBrush 用于在 WPF 窗口中填充 Rectangle。
它通过将其Visual 属性绑定到窗口中的非透明元素来填充。
如何使Rectangle 填充透明,以便通过它可以看到窗口后面的内容,而不使其他元素也透明?
大致遵循创建反射效果的方法on MSDN,并查看有关使用透明窗口here 和here 使特定元素不透明的SO 问题,我在XAML 中提出了以下内容。但是,我似乎无法使 Rectangle 透明,而不使 TextBlock 也必然透明。
<!-- Window.AllowsTransparency is set to true,
so WindowStyle must also be set to None.
Background is set to Transparent so child
elements have capability to be transparent
to what is behind the window. -->
<Window x:Class="XAMLViewTests.TransparentTestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TransparentTestWindow"
AllowsTransparency="True"
WindowStyle="None" Background="Transparent">
<StackPanel Orientation="Vertical">
<!-- Two elements: TextBlock and Rectangle -->
<TextBlock x:Name="textBlock" Background="White">This is some text.</TextBlock>
<Rectangle Height="{Binding ElementName=textBlock, Path=ActualHeight}">
<!-- Fill rectangle with a Visual element bound to TextBlock,
so it shows exactly the same as TextBlock. Transforms, effects, etc
can now be performed on the Visual. -->
<Rectangle.Fill>
<VisualBrush Stretch="None" Visual="{Binding ElementName=textBlock}">
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</Window>
【问题讨论】:
-
你想在这里实现什么?如果将 Fill 设置为一个元素,它将将该元素显示为背景。如果你想要一个透明的矩形,为什么不把它设置为透明呢?
-
矩形背景需要透明,但里面的东西需要不透明或至少部分透明。
-
矩形内到底应该透明什么?文本块的背景?
-
@Dominik 是的,这是正确的。
-
@Aaron:最好贴一张你想要达到的目标的图片。