【发布时间】:2017-01-28 01:22:28
【问题描述】:
<Border ClipToBounds="True" CornerRadius=20>
<Grid>
<Canvas>
<Ellipse Fill="Red" Height="100" Width="100"/>
</Canvas>
<ContentPresenter/>
</Grid>
</Border>
UPD:感谢您的回答。找到解决方案: 使用它除了边框。不幸的是,不透明蒙版不适用于 Canvas 几何图形:(
没有装饰器(即边框)或布局面板(即 Stackpanel) 自带这种开箱即用的行为。
ClipToBounds 用于布局。 ClipToBounds 不会阻止元素 免得越界;它只是防止孩子的布局 来自“溢出”。
此外,大多数元素不需要 ClipToBounds=True,因为 他们的实现不允许他们的内容布局溢出 反正。最值得注意的例外是 Canvas。
最后,Border 认为圆角是内部的绘图 其布局的界限。
public class ClippingBorder : Border
{
protected override void OnRender(DrawingContext dc)
{
OnApplyChildClip();
base.OnRender(dc);
}
public override UIElement Child
{
get
{
return base.Child;
}
set
{
if (Child != value)
{
if (Child != null)
{
// Restore original clipping
Child.SetValue(ClipProperty, _oldClip);
}
if (value != null)
{
_oldClip = value.ReadLocalValue(ClipProperty);
}
else
{
// If we dont set it to null we could leak a Geometry object
_oldClip = null;
}
base.Child = value;
}
}
}
protected virtual void OnApplyChildClip()
{
UIElement child = Child;
if (child != null)
{
_clipRect.RadiusX = _clipRect.RadiusY = Math.Max(0.0, this.CornerRadius.TopLeft - (this.BorderThickness.Left * 0.5));
_clipRect.Rect = new Rect(Child.RenderSize);
child.Clip = _clipRect;
}
}
private RectangleGeometry _clipRect = new RectangleGeometry();
private object _oldClip;
}
【问题讨论】: