使用 VisualBrush 作为 OpacityMask
我想将 OpacityMask
设置为控件,但我需要动态构建该蒙版。 它应该是这样的:
整个(红色)矩形的宽度和高度是动态的,基于父控件的宽度和高度。但我需要在左上角和右上角放置两个小矩形(静态宽度和高度),如图所示。那么我怎样才能做到这一点呢?
我尝试了这段代码,但它不起作用:(根本没有显示任何内容)
<Border BorderBrush="#80FFFFFF" BorderThickness="1" CornerRadius="5">
<Border.OpacityMask>
<VisualBrush>
<VisualBrush.Visual>
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Height="2">
<Border Background="Transparent" Width="12" VerticalAlignment="Stretch" HorizontalAlignment="Left" />
<Border Background="Black" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
<Border Background="Transparent" Width="12" VerticalAlignment="Stretch" HorizontalAlignment="Right" />
</StackPanel>
<Border Background="Black" />
</DockPanel>
</VisualBrush.Visual>
</VisualBrush>
</Border.OpacityMask>
</Border>
这样使用 VisualBrush
是否有效(作为 OpacityMask
)?
I want to set OpacityMask
to a control, but I need to build that mask dynamically.
Here is how it should look:
The width and height of the whole (red) rectangle is dynamic, based on width and height of parent control. But I need to place two small rectangles (static width and height) in top left and top right corner, as shown on image. So how can I make this happen?
I tried this code, but it doesn't work: (nothing is displayed at all)
<Border BorderBrush="#80FFFFFF" BorderThickness="1" CornerRadius="5">
<Border.OpacityMask>
<VisualBrush>
<VisualBrush.Visual>
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Height="2">
<Border Background="Transparent" Width="12" VerticalAlignment="Stretch" HorizontalAlignment="Left" />
<Border Background="Black" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
<Border Background="Transparent" Width="12" VerticalAlignment="Stretch" HorizontalAlignment="Right" />
</StackPanel>
<Border Background="Black" />
</DockPanel>
</VisualBrush.Visual>
</VisualBrush>
</Border.OpacityMask>
</Border>
Is it even valid to use VisualBrush
this way (as a OpacityMask
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你的问题,你希望图像中的那些黑色方块是透明的吗?
更新:在此处上传示例项目:http://www.mediafire.com/?5tfkd1cxwfq0rct
我认为问题在于
VisualBrush
内的Panel
不会拉伸。您可以通过将您使用的任何Panel
的宽度和高度绑定到Border
的 ActualWidth 和 ActualHeight再次更新
Border
的装饰器子代的 DropShadowEffect 似乎推动了Border
垂直和水平方向的 OpacityMask。更糟糕的是,它似乎堆叠起来,因此在您的示例中,当您为三个嵌套的装饰器拥有三个 DropShadowEffects 时,BlurRadius 的总和为 45 (20+15+10),因此 OpacityMask 为推动了 45 的值(至少看起来是这样,但有点难以判断......)。您可以尝试通过增加 ColumnDefinition 宽度和 RowDefinition 高度来弥补这一点,但我认为很难找到动态解决方案。解决您的问题的更好方法可能是使用
Border.Clip
但这也并不容易。更新3
想出了一个更好的解决方案,不需要那么多绑定。创建一个派生自
Border
的自定义类并重写 GetLayoutClip。这在设计器和运行时都有效。为了提高ClippedBorder
的灵活性,您可以引入一些依赖属性来代替硬编码的 2 和 12。此处的新示例应用程序:If I understand your question correctly you want those Black squares in you image to be transparent?
Update: Uploaded sample project here: http://www.mediafire.com/?5tfkd1cxwfq0rct
I think the problem is that the
Panel
inside theVisualBrush
won't stretch. You could get the desired effect by Binding the Width and Height of whateverPanel
you use to the ActualWidth and ActualHeight of theBorder
Update Again
The DropShadowEffect for the Decorator Child of the
Border
seems to push the OpacityMask for theBorder
both Verticaly and Horizontaly. And what's even worse is that it seems to stack, so in your example when you have three DropShadowEffects for three nestedDecorators
, the sum of the BlurRadius is 45 (20+15+10) so the OpacityMask is pushed by a value of 45 (at least it looks like this is whats going on, but it's a little hard to tell..). You could try to compensate for this by increasing the ColumnDefinition Widths and RowDefinition Heights but I think it'll be hard to find a dynamic solution.A better approach to your problem may be to use
Border.Clip
but that doesn't come easy either.Update 3
Came up with a better solution that doesn't require so many Bindings. Create a custom class that derives from
Border
and override GetLayoutClip. This works both in Designer and Runtime. To increase flexibility ofClippedBorder
you could introduce some Dependency Properties to use instead of the hardcoded 2 and 12. New sample app here: http://www.mediafire.com/?9i13rrqpbmzdbvs