WPF Controls Overview - Sample Application
WPF Controls and Event Handlers - C#
In this article we will learn how to use wpf controls like Text Controls, Selection Controls, List Controls, wpf calendar control, wpf combobox, wpf textbox, wpf radio buttons and Other Controls in our wpf application. And we will also do some hands on with WPF Events, Event Handlers and Code Behind.
Related Articles
wpf controls tutorial |
The following c# wpf sample application demonstrates how to work with wpf controls events :-
MainWindow.xaml
<Window x:Class="SimpleForm.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Tutorial - Controls, Events and Event Handlers" Height="450" Width="560">
<Grid Background="#F8F8F8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Name"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Margin="5" />
<TextBox Grid.Column="1"
Name="Name"
Width="250"
Height="20"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Margin="5" />
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Margin="5"
Grid.Row="1"
Text="Gender" />
<StackPanel Orientation="Horizontal"
Grid.Row="1"
Grid.Column="1"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<RadioButton GroupName="Gender"
Name="Male"
Content="Male"
Margin="5"
IsChecked="True" />
<RadioButton GroupName="Gender"
Name="Female"
Content="Female"
Margin="5"/>
</StackPanel>
<TextBlock Text="Latest Mobile"
Grid.Row="2"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Margin="5" />
<StackPanel Orientation="Horizontal"
Grid.Row="2"
Grid.Column="2"
VerticalAlignment="Bottom"
HorizontalAlignment="Left">
<CheckBox Content="Apple"
Name="Apple"
Margin="5" />
<CheckBox Content="Samsung"
Name="Samsung"
Margin="5" />
<CheckBox Content="Google Nexus"
Name="GoogleNexus"
Margin="5" />
</StackPanel>
<TextBlock Text="Designation"
Grid.Row="3"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Margin="5" />
<ComboBox Grid.Row="3"
Grid.Column="1"
Name="Designation"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
SelectionChanged="Job_SelectionChanged"
Margin="5">
<ComboBoxItem Content="Programmer" />
<ComboBoxItem Content="Designer" />
<ComboBoxItem Content="Manager" />
<ComboBoxItem Content="Team Lead" />
<ComboBoxItem Content="CEO" />
</ComboBox>
<TextBlock Text="Product Delivery Date"
Grid.Row="4"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Margin="5" />
<Calendar Grid.Row="4"
Name="DeliveryDate"
Grid.Column="1"
Margin="5"
HorizontalAlignment="Left" />
<Button Content="Save"
Name="SaveButton"
Grid.Row="5"
Margin="5"
HorizontalAlignment="Right"
Click="SaveButton_Click" />
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SimpleForm
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void SaveButton_Click( object sender, RoutedEventArgs e )
{
StringBuilder sb = new StringBuilder();
sb.Append( "Full Name: " );
sb.Append( Name.Text );
sb.Append( " Gender " );
sb.Append( (bool) Male.IsChecked ? "Male" : "Female" );
sb.Append( " Computer: " );
sb.Append((bool)Apple.IsChecked ? "Apple " : "");
sb.Append((bool)Samsung.IsChecked ? "Samsung " : "");
sb.Append((bool)GoogleNexus.IsChecked ? "GoogleNexus " : "");
sb.Append( " Your designation " );
sb.Append( Designation.SelectedItem.ToString() );
sb.Append( " Product Delivery Date " );
sb.Append( DeliveryDate.SelectedDate.ToString() );
MessageBox.Show( sb.ToString() );
}
private void Job_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
var newlySelectedItem = e.AddedItems[0];
MessageBox.Show( newlySelectedItem.ToString() );
}
}
}
WPF Controls Demo
wpf controls events |
I hope you will enjoy the development tips while programming with WPF Controls in C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Also If you like this article, don't forget to share this article with your friends and colleagues.
Also If you like this article, don't forget to share this article with your friends and colleagues.
WPF Controls Overview - Sample Application
Reviewed by Ravi Kumar
on
12:46 AM
Rating:
No comments: