Tuesday, November 16, 2010

More WPF Events and features

The XAML
<Window x:Class="WpfApplication2.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">
<Window.Background>
<ImageBrush />
</Window.Background>
<Grid Background="{x:Null}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="254*" />
<ColumnDefinition Width="249*" />
</Grid.ColumnDefinitions>
<RadioButton Content="Red" Height="16" HorizontalAlignment="Left" Margin="77,56,0,0" Name="radioButton1" VerticalAlignment="Top" Checked="radioButton1_Checked" />
<RadioButton Content="Green" Height="16" HorizontalAlignment="Left" Margin="80,88,0,0" Name="radioButton2" VerticalAlignment="Top" Checked="radioButton2_Checked" />
<RadioButton Content="blue" Height="16" HorizontalAlignment="Left" Margin="80,126,0,0" Name="radioButton3" VerticalAlignment="Top" Checked="radioButton3_Checked" />
<StackPanel Height="119" HorizontalAlignment="Right" Margin="0,48,55,0" Name="stackPanel1" VerticalAlignment="Top" Width="235" Grid.ColumnSpan="2" />
<Button Content="FormBackground" Height="23" HorizontalAlignment="Left" Margin="58,214,0,0" Name="button1" VerticalAlignment="Top" Width="119" Click="button1_Click" MouseEnter="button1_MouseEnter" />
<TextBox Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="237,221,0,0"
Name="textBox1" VerticalAlignment="Top"
Width="120"
GotFocus="textBox1_GotFocus" LostFocus="textBox1_LostFocus"/>
</Grid>
</Window>

The events

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 WpfApplication2
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Background = new SolidColorBrush(Colors.White);
}

private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
stackPanel1.Background = new SolidColorBrush(Colors.Red);
}

private void radioButton2_Checked(object sender, RoutedEventArgs e)
{
stackPanel1.Background = new SolidColorBrush(Colors.Green);
}

private void radioButton3_Checked(object sender, RoutedEventArgs e)
{
stackPanel1.Background = new SolidColorBrush(Colors.Blue);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
this.Background = new LinearGradientBrush(Colors.Goldenrod, Colors.GreenYellow, 50);
}

private void button1_MouseEnter(object sender, MouseEventArgs e)
{
button1.Background = new SolidColorBrush(Colors.Purple);
}

private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
textBox1.Background = new SolidColorBrush(Colors.Navy);
textBox1.Foreground = new SolidColorBrush(Colors.White);
button1.Visibility = Visibility.Collapsed;
}

private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
button1.Visibility = Visibility.Visible;
MessageBox.Show("Come back here!");
}
}
}

No comments:

Post a Comment