[英]Resizable circle button xaml
我正在使用XAML / C#开发适用于Windows的通用应用程序,但无法创建可以调整大小的圆圈按钮。 我使用具有均匀伸展度的Ellipse使其成为圆形,并使用ContentPresenter。
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse Stretch="Uniform">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
问题是均匀的椭圆会自动在顶部,左侧对齐,并且不可能使其延伸网格。 当我调整按钮的大小时,ContentPresenter停留在中间,而椭圆停留在左上角。 我希望能够调整按钮的大小,并使文本停留在圆圈的中心。 感谢帮助!
您可以将路径与圆形EllipseGeometry一起使用:
<ControlTemplate TargetType="Button">
<Grid>
<Path Stretch="Uniform" ...>
<Path.Data>
<EllipseGeometry RadiusX="1" RadiusY="1"/>
</Path.Data>
</Path>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
但是,您将必须显式设置Button的Width或Height,否则它将占用所有可用空间。
我还找到了另一种使用ViewBox的解决方案:
<ControlTemplate TargetType="Button">
<ViewBox>
<Grid>
<Ellipse Stretch="Uniform" Width="50" Height="50">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ViewBox>
</ControlTemplate>
调整大小后,ViewBox会自动缩放所有内容。 您必须设置宽度和高度,但这仅是设置比例。 在Windows和Windows Phone上同时使用用户控件时,此功能非常有用。 感谢您的回答!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.