.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Canvas, Calendar, CheckBox, ComboBox and ContextMenu Control in WPF Day-4

Posted By:Narayanan       Posted Date: August 27, 2012    Points: 200    Category: WPF    URL: http://www.dotnetspark.com  

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>


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;


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



   private void Window_Loaded(object sender, RoutedEventArgs e)




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;


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


            chkNo = new CheckBox();

            chkNo.ClickMode = ClickMode.Press;

            chkNo.Content = "No";

            chkNo.Background = Brushes.LightGreen;

            chkNo.Margin = new Thickness(10, 40, 25, 20);



private void Window_Loaded(object sender, RoutedEventArgs e)




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






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





            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>


                    <MenuItem Header="_USA" /><Separator />

                    <MenuItem Header="_INDIA"  /><Separator />

                    <MenuItem Header="CHINA"/><Separator />

                    <MenuItem Header="SOUTH AFRICA" /><Separator />





Create ContextMenu in Coding:


TextBox txtBox;

   ContextMenu cntMenu;

   private void contentMenus(TextBox txtBox)


            cntMenu = new ContextMenu();




                                cntMenu.Items.Add("SOUTH AFRICA");


            txtBox.ContextMenu = cntMenu;


             private void TextBoxControl()


                                txtBox = new TextBox();

            txtBox.Margin = new Thickness(11, 10, 10, 10);

                                txtBox.Width = 100;

            txtBox.Height = 25;


            this.Content = txtBox;


private void Window_Loaded(object sender, RoutedEventArgs e)





  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.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend