English 中文(简体)
在XAML中采用直径体形式作为单壳
原标题:Use a rectangle shape as a clip in XAML

是否有办法,作为XAML中另一个物体的剪辑的一部分,我可以使用一种正常的矩形(沙皮)。 似乎我能够这样做,但解决办法却让我 el。

<Canvas>

        <Rectangle Name="ClipRect" RadiusY="10" RadiusX="10" Stroke="Black" StrokeThickness="0" Width="32.4" Height="164"/>

<!-- This is the part that I cant quite figure out.... -->
<Rectangle Width="100" Height="100" Clip={Binding ElementName=ClipRect, Path="??"/>

</Canvas>

I know that I can use a RectangleGeometry type approach, but I am more interested in the solution in terms of the code presented above.

最佳回答

Try Shape.RenderedGeometry Property

<Rectangle Width="100" Height="100"
           Clip="{Binding ElementName=ClipRect, Path=RenderedGeometry}" />
问题回答

ClipRect.DefiningGeometry nd ClipRect.RenderedGeometry 仅载有<代码>RadiusXRadiusY。 页: 1

我不敢确定你想要达到什么目标(从你的样本中看,我不清楚),但你可以写一个<代码>。 可从参考<代码>中提取你所要求的信息。 具体内容:

public class RectangleToGeometryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var rect = value as Rectangle;

        if (rect == null || targetType != typeof(Geometry))
        {
            return null;
        }

        return new RectangleGeometry(new Rect(new Size(rect.Width, rect.Height)))
        { 
            RadiusX = rect.RadiusX, 
            RadiusY = rect.RadiusY 
        };
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

之后,你将在你具有约束力的定义中使用这一转换器:

<Rectangle Width="100" Height="100" 
            Clip="{Binding ElementName=ClipRect, Converter={StaticResource RectangleToGeometryConverter}}">

Of course you need to add the converter to your resources first:

<Window.Resources>
    <local:RectangleToGeometryConverter x:Key="RectangleToGeometryConverter" />
</Window.Resources>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...