我感觉可能需要一些转换器,但这就是我想做的。我想要一个单独的Image控件(因为它在绑定到真实数据的数据模板中),具有以下参数:
- 在90x90的空间内居中(不进行任何拉伸)。
- 圆形剪裁半径为40像素(水平和垂直均应该如此)。
- 如果图像> 90x90,则应在90x90的空间中居中,并从中心裁剪一个40x40的圆形。
- 如果图像< 90x90,则应在90x90的空间内居中。剪切圆应没有效果,因为整个图像都包含在裁剪区域内。
<Style x:Key="ImageStyle">
<Setter Property="Width" Value="90" />
<Setter Property="Height" Value="90" />
<Setter Property="Stretch" Value="None" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Clip">
<Setter.Value>
<EllipseGeometry Center="45,45" RadiusX="40" RadiusY="40" />
</Setter.Value>
</Setter>
</Style>
<Grid>
<!-- Other Stuff -->
<Image Source="{Binding ImagePath}" Style="{StaticResource ImageStyle}" />
</Grid>
我可以通过将内容包裹在网格中并在那里移动裁剪来解决第二个问题(小图像裁剪),但是大型内容无法居中:
<Grid>
<!-- Other Stuff -->
<Grid Width="90" Height="90">
<Grid.Clip>
<EllipseGeometry Center="45,45" RadiusX="40" RadiusY="40" />
</Grid.Clip>
<Image Source="{Binding ImagePath}" Style="{StaticResource ImageStyle}" />
</Grid>
</Grid>