在以下示例中,无论何时更改 Grid 大小,剪切区域大小都保持为以绝对坐标表示的大小。
<Grid Clip="M10,10 L10,150 L150,150 L150,10 Z">
<Rectangle Fill="Red"/>
</Grid>
是否可以以某种方式裁剪该区域,以便裁剪几何图形与裁剪对象一起缩放?
不接受解决方案背后的代码,因为这将在控制模板中使用。此外,为了清楚起见,示例中的区域是简单的形状。实际使用的区域是一个复杂且不对称的形状。
编辑:
看起来我必须更具体。这是 ProgressBar 的自定义控件模板的一部分。缩放外部网格时,PART_Indicator 矩形不会缩放其剪切区域。正确的构图是当网格大小为 200x200 时。
<Grid>
<Path Name="PART_Track"
Data="M100,0 A100,100 0 1 0 100,200 A100,100 0 1 0 100,0 Z"
Fill="AliceBlue" Stretch="Fill"/>
<Rectangle Clip="M100,0 A100,100 0 1 0 100,200 A100,100 0 1 0 100,0 Z"
Stretch="Fill"
Name="PART_Indicator" Fill="Red"
Height="100" VerticalAlignment="Top"/>
<Path Name="Border" Data="M100,0 A100,100 0 1 0 100,200 A100,100 0 1 0 100,0 Z"
Stretch="Fill" StrokeThickness="3" Stroke="Black"/>
</Grid>
更新:
Rick 提供了极好的建议,尽管我花了一些时间来理解如何使用它。
这是最终的代码。
<Viewbox StretchDirection="Both" Stretch="Fill" >
<Grid>
<Path Name="PART_Track"
Data="M100,0 A100,100 0 1 0 100,200 A100,100 0 1 0 100,0 Z"
Fill="AliceBlue" Stretch="Fill"/>
<Border Clip="M100,0 A100,100 0 1 0 100,200 A100,100 0 1 0 100,0 Z"
Height="200" VerticalAlignment="Bottom">
<Rectangle Name="PART_Indicator" Fill="Red" VerticalAlignment="Bottom"
Height="40"/>
</Border>
<Path Name="Border"
Data="M100,0 A100,100 0 1 0 100,200 A100,100 0 1 0 100,0 Z"
StrokeThickness="3"
Stretch="Fill" Stroke="Black"/>
</Grid>
</Viewbox>
最佳答案
把 Grid
内部 Viewbox
并更改 Viewbox
的大小而不是 Grid
.
<Viewbox>
<Grid Clip="M10,10 L10,150 L150,150 L150,10 Z" Width="200" Height="200">
<Rectangle Fill="Red"/>
</Grid>
</Viewbox>
关于wpf - 如何使裁剪几何与目标一起缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5956093/