Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用VisualBrush作为不透明掩码_C#_.net_Wpf_Xaml_Visualbrush - Fatal编程技术网

C# 使用VisualBrush作为不透明掩码

C# 使用VisualBrush作为不透明掩码,c#,.net,wpf,xaml,visualbrush,C#,.net,Wpf,Xaml,Visualbrush,我想将OpacityMask设置为控件,但我需要动态构建该掩码。 下面是它的外观: 整个(红色)矩形的宽度和高度是动态的,基于父控件的宽度和高度。但我需要在左上角和右上角放置两个小矩形(静态宽度和高度),如图所示。那么我怎样才能做到这一点呢 我试过这段代码,但不起作用:(根本不显示任何内容) 这样使用VisualBrush是否有效(作为OpacityMask) 更新:此处上载示例项目: 我认为问题在于可视化画笔内的面板不会拉伸。通过将使用的面板的宽度和高度绑定到边框的实际宽度和实际高度,

我想将
OpacityMask
设置为控件,但我需要动态构建该掩码。 下面是它的外观:


整个(红色)矩形的宽度和高度是动态的,基于父控件的宽度和高度。但我需要在左上角和右上角放置两个小矩形(静态宽度和高度),如图所示。那么我怎样才能做到这一点呢

我试过这段代码,但不起作用:(根本不显示任何内容)



这样使用
VisualBrush
是否有效(作为
OpacityMask

更新:此处上载示例项目:

我认为问题在于
可视化画笔内的
面板
不会拉伸。通过将使用的
面板的宽度和高度绑定到
边框的实际宽度和实际高度,可以获得所需的效果

<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>
更新3
提出了一个不需要这么多绑定的更好的解决方案。创建从
Border
派生的自定义类,并重写GetLayoutClip。这在设计器和运行时都有效。为了增加
ClippedBorder
的灵活性,可以引入一些依赖属性来代替硬编码的2和12。新的示例应用程序如下:


是的,黑色方块应该是透明的。我试过你的代码,但它不起作用-它显示了一切。我试图增加行/列的宽度/高度,但它仍然显示内容。@Paja:好的,我上传了我的示例项目,它似乎正在工作。试试看,让我知道有什么不同be@Paja:正在检查,您是否为
边框设置了
Name=“border”
?是的,您是对的,它可以工作。但我不能让它在我的程序中工作-(当边框没有任何附加内容时,它可以工作。一旦我插入带有某些元素的网格,OpacityMask将被忽略。不应该对子元素也有OpacityMask效果?@Paja:我无法重现您的问题,您是对的,它也应该对子元素起作用。我插入了带有一些C的
网格
)孩子们,它仍然在工作。请看我更新的示例代码。您发布了一些示例代码来重现您的问题吗?
<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>
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
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;
    }
}