tags:

views:

475

answers:

2

Exactly as the subject, How to create oval button in WPF application?

+1  A: 

Something like this:

<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>

I did not test the code, but you should get the idea :)

Edit: The Ellipse of course, has a Fill Property instead of Background.

Andrej

Andrej
where does this go in the xaml?
AWC
You can put this in any .Resources section, above the "visual level" of your Button. Every Button beneath will then be oval. For example:<Grid><Grid.Resources><Style...></Grid.Resources><Button Content="Test" /></Grid>
Andrej
A: 

The following code can go directly in your window and supports the IsPressed and IsMouseOver triggers.

<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>
brian