<%@ Language=JavaScript %> myWpfMyApp3

myWpfMyApp3.htm (WPF ) : Border and La

With Visual Studio 2010 and Also Express C# 2010

Objectives:

  • Common WPF Controls
  • Window class
  • Border Properties
  • Label Controls :
    • It is not a container control
    •  Label control can't involve any event
  • Event handling
    • Altering Label and Border Properties
  • Codes

 

Step: 1 Start a new project

Step: Opens MainWindow.xaml and code behind MainWindow.xmal.cs

Step: 2

Add three controls, namely Border (frame like), Label and Button Control.

XAML Code altered

Note Border or Label controls can't harbor any nested control.

Button can invoke method using Event like  "Click" and Command

Label does not have "Click" Event library.

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="350" Width="525">

<Grid>

<Border BorderBrush="Silver" BorderThickness="1" Height="235"

HorizontalAlignment="Left" Margin="20,10,0,0"

Name="border1" VerticalAlignment="Top" Width="427">

<Button Content="Button" Height="47" Name="button1" Width="87"

Click="button1_Click" />

</Border>

<Label Content="Label" Height="53" HorizontalAlignment="Left"

Margin="101,27,0,0" Name="label1"

VerticalAlignment="Top" Width="218" />

</Grid>

</Window>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Boolean clicked = true;
public MainWindow()
{
InitializeComponent();
border1.Background = SystemColors.GrayTextBrush;
String str1 = "My label";
String str2 = border1.Width.ToString();
label1.Content = str1;

}

private void button1_Click(object sender, RoutedEventArgs e)
{

if (clicked == true)
{
label1.Content = "You Clicked a Button";
border1.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
clicked = false;
}
else
{
label1.Content = "Are you trying toggle";
border1.Background = Brushes.FloralWhite;
border1.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
clicked = true;
}
}

}

}
 

Step: 3 Runtime view

A) Checking Button Click

 

Result : expected

  •  bottom alignments of the border control
    • Label content = "You clicked Button"
  • next click should change the background color.
  • Subsequent clicks will change the label contents