vdh_ant

Hi all

I am hoping that someone can help me out. I am wondering if WPF has the capability of successfully applying ClipToBounds (or a property that achieves the same affect) to anything else besides form elements or Panels.

This is because I need to apply this same property to objects like Ellipse, Rectangle (when the RadiusX and RadiusY have set values other than 0) and/or any other shape that can be created with Path. Now I know that the three mentioned elements have the ClipToBounds property but none of these elements allow you to add content of type object, thus the property in this context seems a but superfluous.

In essence this is what I would like to be able to do:

<Rectangle Fill="{x:Null}" Stroke="#FF000000" RadiusX="18.5" RadiusY="18.5" Height="40" Width="40" />

<Ellipse Fill="#FFFFFFFF" Stroke="#FF000000" Height="50" Width="50" />

</ Rectangle>

Hence an Ellipse is Clipped To the Bounds of the smaller square that has rounded corners.

Also i was just wondering if I pay a high penalty for using clipping

If someone could help me out with this that would be great

Thanks

Anthony




Re: Windows Presentation Foundation (WPF) ClipToBounds property on Ellipse, Rectangle and Path

vdh_ant

Anyone got any ideas on this one

Thanks
Anthony






Re: Windows Presentation Foundation (WPF) ClipToBounds property on Ellipse, Rectangle and Path

SerialSeb

You may want to have a look at the Clip property. To clip your ellipse to a geometry you'd do the following:

<Ellipse Fill="White" Stroke="Black" Height="50" Width="50">
<Ellipse.Clip>
<RectangleGeometry RadiusX="18.5" RadiusY="18.5" Rect="0,0,40,40" />
</Ellipse.Clip>
</Ellipse>

If you want both a rectangle for the outside border and the ellipse, put them in a canvas like so:

<Canvas>
<Rectangle Stroke="Black" Canvas.Top="0" Canvas.Left="0" RadiusX="18.5" RadiusY="18.5" Width="40" Height="40"/>

<Ellipse Fill="Red" Stroke="#FF000000" Height="50" Width="50" Canvas.Top="0" Canvas.Left="0">
<Ellipse.Clip>
<RectangleGeometry RadiusX="18.5" RadiusY="18.5" Rect="0,0,40,40" />
</Ellipse.Clip>
</Ellipse>
</Canvas>






Re: Windows Presentation Foundation (WPF) ClipToBounds property on Ellipse, Rectangle and Path

vdh_ant

Thanks SerialSeb for the reply

But this really isn't the type of functionality that I am after. The Clip property seems to be more of a selection tool (as in what part of the ellipse to I want to show) rather than a way of saying 'i have these 4 different images, shapes, movies, etc that I don¡¯t want to show out side the boundary of the container that holds them'. Were the containers its self can be any Geometric shape When working this would act the exact same way as ClipToBounds does with Panels and Boarders, but both of those objects can only be rendered as shapes with 90 degree corners. Now I know that all UIElements have ClipToBounds but it doesn¡¯t make any sense to me why Ellipse, Rectangle and Path do when you can¡¯t have anything inside these objects. If you could, then it would make logical sense and my problem would be solved. But as it stands the property does nothing¡­ So does ClipToBounds have any use on an Ellipse, Rectangle and/or Path

So given all this and the fact that ClipToBounds doesn¡¯t seem to work in the context of an Ellipse, Rectangle and/or Path, is there another way of doing what I need to do so that actually works on non rectangular objects.

Thanks
Anthony






Re: Windows Presentation Foundation (WPF) ClipToBounds property on Ellipse, Rectangle and Path

SerialSeb

Anthony,

that's why i suggest drawing the outside geometry first, then clipping the inside geometry with an equivalent shape. You can also use other geometry, constructs and intersections, which is documented on msdn here.






Re: Windows Presentation Foundation (WPF) ClipToBounds property on Ellipse, Rectangle and Path

vdh_ant

Thanks again for the reply SerialSeb

But this seems like a very very round about way of hacking the elements to fake an effect. It wouldn't be so bad if the elements on my page were static (i.e. not moving), but as soon as i want to animate the elements that fall within this imaginary shape, the system falls apart.

Besides all of that it doesn't explain what an Ellipse, Rectangle and Path have the ClipToBounds property when it appears to do nothing. It would only work if you could nest objects within these given elements, but since you can not the property is useless. As far as i can see the whole point of ClipToBounds is so that you dont have to worry about making sure that one element has a clip set and then another element is laid over the top. The point is that you don¡¯t have to worry about that, because when it comes to animation it would be near impossible and very impractical to continuously maintain that illusion. Thus the reason for ClipToBounds imho.

Can you or anyone else out there explain to me why Ellipse, Rectangle and Path have the ClipToBounds property when you can't place elements within it

Thanks all
Anthony






Re: Windows Presentation Foundation (WPF) ClipToBounds property on Ellipse, Rectangle and Path

SerialSeb

The answer is in msdn:

FrameworkElement overrides the metadata for this dependency property. Specifically, FrameworkElement designates this property to allow property value inheritance (Inherits is true in metadata). Property value inheritance in this context means that if there are child elements with no other value for ClipToBounds assigned through local values or styles, the value of the nearest parent element with this value assigned (again, either in styles, by default values, or a locally value), then the value from the parent element will be assigned to all unassigned child elements by the property system. In practice this means that you can specify whether to allow clip to bounds at the root element, and that value will propagate to all child elements that have not specifically assigned it as false.

As it stands, Shape inherits from FrameworkElement and as such inherits that property. But without children, it is indeed pretty useless. Not all elements inherited from FrameworlElement have children, however clipping on Width/Height will always happen whatever the size of your Shape.

Hope it helps.






Re: Windows Presentation Foundation (WPF) ClipToBounds property on Ellipse, Rectangle and Path

vdh_ant

Ok that makes a bit more sense now.

So given every that we have gone through does this mean that there is NO way using a given property to produce the same results at ClipToBounds if you want to do this on any other space that does not have 90 degree corners

Thanks
Anthony






Re: Windows Presentation Foundation (WPF) ClipToBounds property on Ellipse, Rectangle and Path

SerialSeb

Well, graphics operations like XOR and intersections are common. Take the case of intersecting two polygons. What's really going on is first defining the visible part of the combined geometry, and that is what the clipping (and the Clip property) is all about. The ClipToBounds means the content will be clipped based on the bounds of the object, a geometry that happens to start at 0,0 and ends at Width,Height.

I'm sure you could simply write a Shape element, add a Child property, and automatically assign the Clip property of the child element to the available size of the parent, to have the effect you're looking for. I still think drawing two geometries and clipping one of them is the most achievable way of doing what you want, as you imply you want to animate.

But maybe someone else would have another point of view