Introduction:
Last Three days, we talked about WPF
Introduction, AccessText Control and Board . Today we will discuss about Canvas, Calendar, CheckBox, ComboBox and
ContextMenu Control.
· Canvas Control:
It is a Layout panel.
A Canvas panel is used to position child elements by using coordinates
that are relative to the canvas area.
Create Canvas in XAML:
<Canvas Background="BurlyWood" Margin="88,0,120,99" Height="66"
VerticalAlignment="Bottom">
<Button Name="btn_can" Margin="15,18,70,70" Height="26" Width="40" Canvas.Left="-1" Canvas.Top="17">Cancel</Button>
<Button Height="23" Margin="15,50,75,50" Name="btn_Ok" VerticalAlignment="Bottom" Click="btn_OK_Click" Width="39.893" Canvas.Left="-1" Canvas.Top="-44">OK</Button>
</Canvas>
I just declare Buttons with in Canvas Tag.
Create
Canvas in Coding:
Canvas
cans;
private
void canvas()
{
cans
= new Canvas();
cans.Width =
150;
cans.Height
= 150;
cans.Background = Brushes.Lavender;
canvascontrols(cans);
this.Content
= cans;
}
private void
canvascontrols(Canvas cntCon)
{
btn_Exit = new Button();
btn_Exit.Background = Brushes.Ivory;
btn_Exit.Content = "Exit";
btn_Exit.Name = "ButtonExit";
btn_Exit.Height = 30;
btn_Exit.Width = 50;
btn_Exit.Margin = new Thickness(10,
10, 20, 50);
btn_Exit.Click += new RoutedEventHandler(btn_Exit_Click);
cntCon.Children.Add(btn_Exit);
}
private void Window_Loaded(object
sender, RoutedEventArgs e)
{
canvas();
}
Here, I created Method (canvascontrols) for adding a Button into Canvas.
· Calendar Control:
Enable a user to select a date by using a
visual calendar display.
It starts from .Net Framework 4.5.
Please
refer this:
http://msdn.microsoft.com/en-us/library/system.windows.controls.calendar.aspx
· CheckBox Control:
User can select and clear.
Create
Canvas in XAML:
<CheckBox Content="Yes" Background="Khaki" IsChecked="True" ></CheckBox>
Create
Canvas in Coding:
CheckBox chkYes;
CheckBox chkNo;
private void canvas()
{
cans = new
Canvas();
cans.Width = 150;
cans.Height = 150;
cans.Background = Brushes.Lavender;
checkYesNoControls(cans);
this.Content
= cans;
}
private void checkYesNoControls(Canvas
cntCon)
{
chkYes = new CheckBox();
chkYes.ClickMode = ClickMode.Press;
chkYes.Content = "Yes";
chkYes.Background = Brushes.Khaki;
chkYes.IsChecked = true;
chkYes.Margin = new Thickness(10,
10, 25, 20);
cntCon.Children.Add(chkYes);
chkNo = new
CheckBox();
chkNo.ClickMode = ClickMode.Press;
chkNo.Content = "No";
chkNo.Background = Brushes.LightGreen;
chkNo.Margin = new Thickness(10,
40, 25, 20);
cntCon.Children.Add(chkNo);
}
private void
Window_Loaded(object sender, RoutedEventArgs e)
{
canvas();
}
· ComboBox Control:
Selection control with a drop-down
list that can be shown or hidden by clicking the arrow on the control.
Create
ComboBox in XAML:
<ComboBox Height="23" Margin="60,76,98,0" Name="comboBox1" VerticalAlignment="Top">
<ComboBoxItem>IBM</ComboBoxItem>
<ComboBoxItem>GANTEC</ComboBoxItem>
<ComboBoxItem>INTEL</ComboBoxItem>
<ComboBoxItem>HP</ComboBoxItem>
</ComboBox>
Create
ComboBox in Coding:
ComboBox
cmbCompany;
private void comboBoxControls()
{
cmbCompany = new ComboBox();
cmbCompany.Width = 100;
cmbCompany.Height = 20;
cmbCompany.Margin = new Thickness(10,
5, 10, 20);
cmbCompany.Items.Add("IBM");
cmbCompany.Items.Add("SATHYAM");
cmbCompany.Items.Add("KRISH");
cmbCompany.Items.Add("GANTEC");
this.Content
= cmbCompany;
}
private void
Window_Loaded(object sender, RoutedEventArgs e)
{
comboBoxControls ();
}
User can’t change the items in the ComboBox.It
likes DropdownStyle in Windows Form
· ContextMenu Control:
Represents a pop-up menu.
Control to expose
functionality that is specific to the context of the control.
The
following error message will show when user drag and drop a Context Menu in the
window.
Create
ContextMenu in XAML:
<TextBox Name="textBox1" TextWrapping="Wrap" Margin="10, 10, 5, 5" Grid.Row="7"> <TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="_USA" /><Separator />
<MenuItem Header="_INDIA" /><Separator />
<MenuItem Header="CHINA"/><Separator />
<MenuItem Header="SOUTH
AFRICA" /><Separator />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
Create
ContextMenu in Coding:
TextBox txtBox;
ContextMenu
cntMenu;
private void
contentMenus(TextBox txtBox)
{
cntMenu = new ContextMenu();
cntMenu.Items.Add("INDIA");
cntMenu.Items.Add("USA");
cntMenu.Items.Add("CHINA");
cntMenu.Items.Add("SOUTH AFRICA");
cntMenu.Items.Add("JAPAN");
txtBox.ContextMenu = cntMenu;
}
private
void TextBoxControl()
{
txtBox = new TextBox();
txtBox.Margin = new Thickness(11, 10, 10, 10);
txtBox.Width = 100;
txtBox.Height = 25;
contentMenus(txtBox);
this.Content = txtBox;
}
private void
Window_Loaded(object sender, RoutedEventArgs e)
{
TextBoxControl();
}
Conclusion:
I hope, you get some ideas about Button and BulletDecorator Control in WPF.
Thanks for reading this article. We will talk about other controls in next
Chapters.