ClipRect.DefiningGeometry
nd ClipRect.RenderedGeometry
僅包含RadiusX
和RadiusY
值,但不包括Rect
。
我不知道究竟你要實現(這不是很清楚,我從你的樣品)什麼,但你可以寫一個IValueConverter
這將提取您從引用Rectangle
需要的信息:
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}}">
當然,你需要轉換器第一次添加到您的資源:
<Window.Resources>
<local:RectangleToGeometryConverter x:Key="RectangleToGeometryConverter" />
</Window.Resources>
看看http://stackoverflow.com/questions/1321740/wpf-how-to-show-only-part-of-big-canvas – 2012-04-09 15:28:57