使用 VisualBrush 作为 OpacityMask

2023-12-08

我想设置OpacityMask到一个控件,但我需要动态构建该掩码。 它应该是这样的:


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?

我尝试了这段代码,但它不起作用:(根本没有显示任何内容)

<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)?


如果我正确理解你的问题,你希望图像中的那些黑色方块是透明的吗?

Update:在此处上传示例项目:http://www.mediafire.com/?5tfkd1cxwfq0rct

我认为问题在于Panel在 - 的里面VisualBrush不会拉伸。您可以通过绑定任何内容的宽度和高度来获得所需的效果Panel您使用的 ActualWidth 和 ActualHeightBorder

<Border Name="border" BorderBrush="Red" BorderThickness="1" CornerRadius="5">
    <Border.OpacityMask>
        <VisualBrush>
            <VisualBrush.Visual>
                <Grid Width="{Binding ElementName=border, Path=ActualWidth}"
                      Height="{Binding ElementName=border, Path=ActualHeight}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="20"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Rectangle Fill="Transparent" Grid.Column="0"/>
                    <Rectangle Fill="Black" Grid.Column="1"/>
                    <Rectangle Fill="Transparent" Grid.Column="2"/>
                    <Rectangle Fill="Black" Grid.Row="1" Grid.ColumnSpan="3"/>
                </Grid>
            </VisualBrush.Visual>
        </VisualBrush>
    </Border.OpacityMask>
    <Grid>
        <TextBlock Text="Testing OpacityMask with a rather long string................." Grid.ZIndex="3"/>
        <Rectangle Fill="Green"/>
    </Grid>
</Border>

再次更新
装饰器子级的 DropShadowEffectBorder似乎推动了 OpacityMask 的Border垂直和水平。更糟糕的是,它似乎会堆叠,因此在您的示例中,当您为三个嵌套设置三个投影效果时Decorators,BlurRadius 的总和为 45 (20+15+10),因此 OpacityMask 的值被推高了 45(至少看起来是这样,但有点难以判断......)。您可以尝试通过增加 ColumnDefinition 宽度和 RowDefinition 高度来弥补这一点,但我认为很难找到动态解决方案。

解决您的问题的更好方法可能是使用Border.Clip但这也并不容易。

Point1:  0, 2
Point2: 12, 2
Point3: 12, 0
Point4: Width of Border - 12, 0
Point5: Width of Border - 12, 2
Point5: Width of Border, 2
Point6: Width of Border, Height of Border
Point7: 0, Height of Border

Update 3
想出了一个更好的解决方案,不需要那么多绑定。创建一个派生自的自定义类Border并重写 GetLayoutClip。这在设计器和运行时都有效。为了增加灵活性ClippedBorder您可以引入一些依赖属性来代替硬编码的 2 和 12。这里有新的示例应用程序:http://www.mediafire.com/?9i13rrqpbmzdbvs

public class ClippedBorder : Border
{
    protected override Geometry GetLayoutClip(Size layoutSlotSize)
    {
        PathGeometry pathGeometry = new PathGeometry();
        pathGeometry.Figures = new PathFigureCollection();

        //Point1:  0, 2
        PathFigure pathFigure = new PathFigure();
        pathFigure.StartPoint = new Point(0, 2);

        //Point2: 12, 2
        LineSegment lineSegment1 = new LineSegment();
        lineSegment1.Point = new Point(12, 2);

        //Point3: 12, 0
        LineSegment lineSegment2 = new LineSegment();
        lineSegment2.Point = new Point(12, 0);

        //Point4: Width of Border - 12, 0
        LineSegment lineSegment3 = new LineSegment();
        lineSegment3.Point = new Point(this.ActualWidth-12, 0);

        //Point5: Width of Border - 12, 2
        LineSegment lineSegment4 = new LineSegment();
        lineSegment4.Point = new Point(this.ActualWidth-12, 2);

        //Point5: Width of Border, 2
        LineSegment lineSegment5 = new LineSegment();
        lineSegment5.Point = new Point(this.ActualWidth, 2);

        //Point6: Width of Border, Height of Border
        LineSegment lineSegment6 = new LineSegment();
        lineSegment6.Point = new Point(this.ActualWidth, this.ActualHeight);

        //Point7: 0, Height of Border 
        LineSegment lineSegment7 = new LineSegment();
        lineSegment7.Point = new Point(0, this.ActualHeight);

        pathFigure.Segments.Add(lineSegment1);
        pathFigure.Segments.Add(lineSegment2);
        pathFigure.Segments.Add(lineSegment3);
        pathFigure.Segments.Add(lineSegment4);
        pathFigure.Segments.Add(lineSegment5);
        pathFigure.Segments.Add(lineSegment6);
        pathFigure.Segments.Add(lineSegment7);

        pathGeometry.Figures.Add(pathFigure);

        return pathGeometry;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 VisualBrush 作为 OpacityMask 的相关文章

随机推荐

  • 如何让一个进程等待多个资源?

    我目前正在使用 SimPy 来建模和模拟服务器进程 我希望该进程根据从何处接收此消息来执行不同的操作 SimPy 文档展示了如何等待多个事件 例如 yield event1 事件2 不过 我目前正在尝试等待多个商店提供资源 场景如下 服务器
  • 将测试结果添加到 VSTS 中的测试运行(测试用例)

    我需要将测试结果添加到 VSTS 中的测试用例中 我是 VSTS 新手 不确定我的代码出了什么问题 var ur new Uri https myaccount visualstudio com VssCredentials cr new
  • Vaadin 7 在组件之间触发自定义事件

    我想创建自定义事件并在视图的某些部分触发它们 以便更新 删除 刷新视图的其他部分 我尝试过扩展 Component Event 和 Component Listener 但它不起作用 我认为事件和侦听器必须仅限于同一组件实例 Vaadin
  • Cython 条件编译基于通过“setuptools”给出的外部值

    我尝试从 Cython pyx 文件有条件地生成 C 代码 我在 Cython 文档中找到了我可以使用的DEF定义一个值和IF根据定义的值有条件地生成代码 但是如何从setup py via Extension from setuptool
  • 如何从离子应用程序中删除闪屏

    直接启动应用程序 不会出现闪屏和白屏 我正在开发一个离子应用程序 我需要在加载应用程序之前删除应用程序启动时显示的启动屏幕 我不想要任何闪屏 需要删除完整的闪屏不知道该怎么做 让我展示一下我的应用程序的机器设置 Your system in
  • Breeze.Server.WebAPI2“无法满足包依赖性约束”

    我正在尝试添加Breeze Server WebAPI2Nuget 包到 Visual Studio Community 2015 RC 当我单击安装时 我收到 无法满足包依赖性约束 列出的依赖项是 Microsoft AspNet Web
  • 重启Android Studio

    如何重启Android Studio来克服Gradle项目刷新失败错误 无法在以下位置找到哈希字符串 Google Inc Google APIs 23 的目标 C Users Admin AppData Local Android sdk
  • html表格的浮动水平滚动条

    我有一个非常高的 html 表格 网页必须垂直滚动才能到达表格底部的滚动条 当用户滚动页面并且表格可见时 如果我可以将表格的水平滚动条浮动在浏览器窗口底部 那就太好了 这样的事可以做吗 这是 jsFiddle 的情况示例 http jsfi
  • 缩短/避免 if 语句中级联空检查的方法

    我有这个条件 if Model Bids null Model Bids Items null Model Bids Items Count gt 0 问题是 我认为这很丑陋 我可以编写一个封装此函数的函数 但我想知道是否还有其他东西可以帮
  • 如何使用 Google Apps 脚本从 Google 电子表格单元格获取 url?

    我有一个从 Excel 复制的谷歌电子表格 我对此有一些问题 这是我的文件https docs google com spreadsheets d 1Ok phu5OXtvKHLj3MLa7N3WV2qBdMWRz8dLHnTqjHrc e
  • Crystal Reports 图像在 Web 查看器中不可见

    我在继承的应用程序 NET 1 1 中有一些 Crystal Reports V10 该应用程序部署在四个 相同 环境中 在其中三种环境中 它们运行良好 在第四种情况下 图表图形在 Web 查看器中不可见 如果导出报告 它们是可见的 IT
  • 验证日期时间选择器的输入

    如何在 XML 视图中为 datetimepicker 注册验证错误回调 以及如何因无效日期输入而触发此事件 The 日期时间选择器控件是一个带有弹出日期选择器的输入框 用户可以直接在输入中键入或使用所选日期来选择日期 我可以向日期时间值添
  • “Microsoft.ACE.OLEDB.12.0”64x Sql Server 和 86x Office?

    错误 OLE DB 提供程序 Microsoft ACE OLEDB 12 0 不能用于分布式查询 因为该提供程序配置为在单线程单元模式下运行 我看到的答案是 64 位 Sql Server 和 32 位 Office 之间存在冲突 有没有
  • 如何在android中将单个字符串转换为JsonArray?

    我需要将 String 转换为 JsonArray 但我不知道如何转换 我是 Android 开发新手 我想在 MySQL 数据库中插入通话记录详细信息 所以 从 android 端我得到一个字符串 但我不知道如何将该字符串转换为 Json
  • 如何旋转这个 openGl 代码

    在这段代码中 我尝试绘制简单的奥林匹克环并旋转它 下面的工作正常 但我无法旋转环 帮助我解决这个问题 void myReshape int width int height glViewport 0 0 width height glMat
  • 什么是抑制异常?

    一条评论 由用户soc on an answer to 关于尾调用优化的一个问题提到Java 7有一个新功能叫做 抑制异常 因为 ARM的加入 支持ARM CPU 在这种情况下 什么是 受抑制的异常 在其他情况下 抑制的异常 是捕获然后忽略
  • 致命错误:未捕获错误:调用未定义的函数 mysql_pconnect()

    我在 Codeigniter 中遇到这些错误 Fatal error Uncaught Error Call to undefined function mysql pconnect in C xampp1 htdocs CI system
  • xtsible 对象,在 quantmod 中循环

    我想循环遍历股票代码列表并使用以下命令打印它们chartSeries 这比总是改变论点要容易得多 不幸的是 当我想要循环或子集时 我总是会收到错误 Error in try xts x error chartSeries requires
  • CUDA 内核调用中的隐式构造函数

    我正在尝试将一些 POD 传递给内核 该内核具有一些非 POD 作为参数 并且具有非显式构造函数 其背后的想法是 在主机上分配一些内存 将内存传递给内核 并将内存封装在对象中 而无需用户显式执行该步骤 构造函数被标记为 device 代码
  • 使用 VisualBrush 作为 OpacityMask

    我想设置OpacityMask到一个控件 但我需要动态构建该掩码 它应该是这样的 The width and height of the whole red rectangle is dynamic based on width and h