<Application x:Class="SimpleXamlApp.MyApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" Exit ="AppExit"
>
<x:Code>
void AppExit(object sender, ExitEventArgs e)
{
MessageBox.Show("App has exited");
}
</x:Code>
</Application>
Month: February 2011
Set button properties with Linq style
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="250" Width="250" Loaded="Window_Loaded">
<Canvas Name="canvas1">
</Canvas>
</Window>
//File:Window.xaml.cs
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
{
public partial class Window1 : Window
{
Button button1 = null;
Button button2 = null;
Button button3 = null;
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
button1 = new Button { Content = "Button", Width = 70, Height = 23 };
Canvas.SetLeft(button1, 119);
Canvas.SetTop(button1, 24);
canvas1.Children.Add(button1);
button2 = new Button { Content = "Wider" };
Canvas.SetLeft(button2, 44);
Canvas.SetTop(button2, 69);
canvas1.Children.Add(button2);
button3 = new Button { Content = "Button" };
Canvas.SetLeft(button3, 78);
Canvas.SetTop(button3, 119);
button3.Padding = new Thickness(10, 2, 10, 2);
canvas1.Children.Add(button3);
}
}
}
Xaml and Code behind
<Window
x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hello, WPF">
<Button x:Name="button" Width="200" Height="25" Click="button_Click">Click me</Button>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApplication1{
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}
void button_Click(object sender, RoutedEventArgs e) {
MessageBox.Show("You", "Nice!");
}
}
}
Use Linq to get control from a container
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" SizeToContent="Height" Width="300">
<Grid Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="Gray" BorderThickness="1" />
<Border Grid.Column="1" BorderBrush="Gray" BorderThickness="1" />
<StackPanel Grid.Column="0" HorizontalAlignment="Center" Margin="5" Name="spLeftContainer">
<TextBlock FontSize="16" Text="Radio Group 1" />
<RadioButton Content="Radio Button 1A" GroupName="Group1" IsChecked="True" Margin="5" Name="rbnOneA" />
<RadioButton Content="Radio Button 1B" GroupName="Group1" Margin="5" Name="rbnOneB" />
<RadioButton Content="Radio Button 1C" GroupName="Group1" Margin="5" Name="rbnOneC" />
<Separator/>
<TextBlock FontSize="16" Text="Radio Group 2" />
<RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2A" IsChecked="True"
Margin="5" Name="rbnTwoA" />
<RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2B" Margin="5" Name="rbnTwoB"/>
<RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2C" Margin="5" Name="rbnTwoC"/>
</StackPanel>
<StackPanel Grid.Column="1" HorizontalAlignment="Center" Margin="5" Name="spRightContainer">
<TextBlock FontSize="16" Text="Radio Group 1" />
<RadioButton Content="Radio Button 1D" GroupName="Group1" Margin="5" Name="rbnOneD" />
<RadioButton Content="Radio Button 1E" GroupName="Group1" Margin="5" Name="rbnOneE" />
</StackPanel>
<Button Content="Show Group1 Selection" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center"
Margin="10" MaxHeight="25" Click="Button_Click" />
</Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
RadioButton radioButton = null;
radioButton = GetCheckedRadioButton(spLeftContainer.Children, "Group1");
if (radioButton == null)
{
radioButton = GetCheckedRadioButton(spRightContainer.Children, "Group1");
}
MessageBox.Show(radioButton.Content + " checked.", Title);
}
private RadioButton GetCheckedRadioButton(UIElementCollection children, String groupName)
{
return children.OfType<RadioButton>().FirstOrDefault( rb => rb.IsChecked == true && rb.GroupName == groupName);
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
if (!this.IsInitialized) return;
RadioButton radioButton = e.OriginalSource as RadioButton;
if (radioButton != null)
{
MessageBox.Show(radioButton.Content + " checked.", Title);
}
}
}
}
My First WPF App with code behind
<Window x:Class="MyFirstWPFApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyFirstWPFApp" Height="336" Width="343">
<Grid>
<Button Click="MyClickEvent" VerticalAlignment="Top" HorizontalAlignment="Left"
Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1" Margin="43,61,0,0"
Width="75" Height="23" Name="btnGo">
Go
</Button>
</Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;
namespace MyFirstWPFApp
{
public partial class Window1 : System.Windows.Window
{
private void MyClickEvent(object sender, RoutedEventArgs e)
{
MessageBox.Show("A!", "Message", MessageBoxButton.OK, MessageBoxImage.Hand);
}
public Window1()
{
InitializeComponent();
}
}
}
Create Button from Xaml string
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DynamicXAML" Height="300" Width="300">
<Grid Name="grid1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Markup;
using System.Xml;
using System.IO;
namespace WpfApplication1
{
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
StringReader sr = new StringReader(@"<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Foreground='BurlyWood' FontSize='20pt'>Click Me!</Button>");
XmlReader reader = XmlReader.Create(sr);
Button dynamicButton = (Button)XamlReader.Load(reader);
this.grid1.Children.Add(dynamicButton);
dynamicButton.Click += button1_Click;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Dynamic Button Loaded From XAML String");
}
}
}
Reference name defined in Xaml in cs file
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.IndexOfSample"
WindowTitle="UIElementCollection IndexOf Sample">
<DockPanel Name="ParentElement">
<TextBlock DockPanel.Dock="Top" Name="TxtDisplay"></TextBlock>
<Button DockPanel.Dock="Top" Click="FindIndex">What is the Index Number
of the Element Just Added?</Button>
<DockPanel Name="MainDisplayPanel">
<TextBlock DockPanel.Dock="Top">Text 1</TextBlock>
<TextBlock DockPanel.Dock="Top">Text 2</TextBlock>
</DockPanel>
</DockPanel>
</Page>
//File:Window.xaml.cs
namespace WpfApplication1 {
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
public partial class IndexOfSample {
void FindIndex(object sender, RoutedEventArgs e)
{
TextBlock newText = new TextBlock();
MainDisplayPanel.Children.Add(newText);
newText.Text = "New element #" ;
DockPanel.SetDock(newText,Dock.Top);
TxtDisplay.Text = MainDisplayPanel.Children.IndexOf(newText)+"";
}
}
}

