01 Hello World

WPF Tutorial 01 – Hello World

  1. Create a new empty Visual Studio Solution: WPFTutorials
  2. Add to the solution a new C#, Avalon Application: 01_HelloWorld
  3. Click Start to see what VS2005 create for you
  4. Insert in the Grid tag in Window1.xaml a label like this:

    <Label>Hello World</Label>
     

  5. Click start to test the application
  6. Add two attributes to the Grid to make the label appear in the middle of the
    Window:

    VerticalAlignment="Center" HorizontalAlignment="Center"
     

  7. Start the application and resize the window
  8. Add a button tag to the Grid:

    <Button>Ok</Button>
     

  9. Start the application to see how the button hides the label
  10. Surround the label and button in a new Grid and add row and column
    definitions like this:

    <Grid ShowGridLines="True">
      <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
       <ColumnDefinition></ColumnDefinition>
       <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>
      <Label Grid.Column="0" Grid.Row="0">Hello World</Label>
      <Button Grid.Column="1" Grid.Row="1">Ok</Button>
    </Grid>
     

  11. Start the application to see how the label and button are aligned
  12. Add a click attribute to the button:

    <Button Grid.Column = "1" Grid.Row = "1" Click = "OkButtonClick" >Ok</Button>
     

  13. Add an eventhandler to the Window1.xaml.cs file directly under the sample
    eventhandler inside the Window1 class:

    private void OkButtonClick(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Hello WPF!");
    }

     

  14. Start the application and click the button