Button control in WPF
Button:
Namespace : System.Windows.Controls.Button
Designer Code:
<!--Window1.XAML-->
<Button Click="OnButtonClicked">Click me!</Button>
Code Behind:
// Window1.xaml.cs
private void OnButtonClicked(object sender, EventArgs e){
MessageBox.Show("Clicked!");
}
This is a ContentControl.
For image property add in a stack panel, an image element and a text element.
Button with Image:
<Button Name="ImageBtn" Width="30" Height="20" Click="OnClickButton">
<Image Source="Path\Image.jpg"></Image>
</Button>
void OnClickButton(object sender, RoutedEventArgs e)
{
ImageBtn.Content = "Image Test";
ImageBtn.Background = Brushes.Blue;
}
For cancel buttons, set IsCancel=”true”
For accept (OK) buttons, set IsDefault=”true”
Setting Button Template:
<Button x:Name="BtnWithTemplate">
<Button.Template>
<ControlTemplate>
<Border HorizontalAlignment="Center" VerticalAlignment="Center" >
<Image Source="Path\Image.jpg" Width="20" Height="20"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
Controls inside Button :
<Button x:Name="Samaple"
HorizontalContentAlignment="Stretch"
Padding="0"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Width="200"
Height="200">
<Grid >
<StackPanel>
<Label Width="200" Height="26" Content="Image test"/>
<Image Width="190" Height="30" Source="Path\win2wpf.JPG" />
</StackPanel>
</Grid>
</Button>