正如主题一样,如何在WPF应用程序中创建椭圆形按钮?
答案 0 :(得分:4)
这样的事情:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Width="64" Height="32" Fill="Blue" />
<ContentControl Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我没有测试代码,但你应该明白:)
编辑:椭圆当然有一个填充属性而不是背景。
的Andrej
答案 1 :(得分:1)
以下代码可直接在您的窗口中显示,并支持IsPressed和IsMouseOver触发器。
<Window.Resources>
<ControlTemplate x:Key="ButtonControlTemplate" TargetType="{x:Type Button}">
<Grid>
<Ellipse Fill="White" Stroke="Black" VerticalAlignment="Top" Height="32" x:Name="theEllipse"/>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" Value="Yellow" TargetName="theEllipse"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Fill" Value="Gray" TargetName="theEllipse"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Content="Button" Template="{DynamicResource ButtonControlTemplate}"/>
</Grid>