Intro
XAML
Layout
Dependency Properties
Routed Events
<Window x:Class="WpfBasics.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>
</Grid>
</Window>
Title="MainWindow" Height="350" Width="525"
x:Class="WpfBasics.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
// <Window x:Class="WpfBasics.MainWindow"></Window>
namespace WpfBasics {
public partial class MainWindow : Window {
public MainWindow() { InitializeComponent(); }
}
}
<Grid x:Name="MyGrid"></Grid>
// or
<Grid Name="MyGrid"></Grid>
<TextBox Name="txtQuestion" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
FontFamily="Verdana" FontSize="24" Foreground="Green" ..> </TextBox>
<Grid x:Name="mainGrid">
<Grid.Background>// ... </Grid.Background>
</Grid>
<Grid x:Name="mainGrid">
<Grid.Background>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.00" Color="Red" />
<GradientStop Offset="0.50" Color="Indigo" />
<GradientStop Offset="1.00" Color="Violet" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Grid.Background>
// ...
</Grid>
LinearGradientBrush brush = new LinearGradientBrush();
GradientStop gradientStop1 = new GradientStop();
gradientStop1.Offset = 0;
gradientStop1.Color = Colors.Red;
brush.GradientStops.Add(gradientStop1);
GradientStop gradientStop2 = new GradientStop();
gradientStop2.Offset = 0.5;
gradientStop2.Color = Colors.Indigo;
brush.GradientStops.Add(gradientStop2);
GradientStop gradientStop3 = new GradientStop();
gradientStop3.Offset = 1;
gradientStop3.Color = Colors.Violet;
brush.GradientStops.Add(gradientStop3);
mainGrid.Background = brush;
<Button Foreground="{x:Static SystemColors.ActiveCaptionBrush}">
<Button>
<Button.Foreground>
<x:Static Member="SystemColors.ActiveCaptionBrush"></x:Static>
</Button.Foreground>
</Button>
<Button Grid.Row=”1”>Ok</Button>
// <Button Click="cmdAnswer_Click"></Button>
private void cmdAnswer_Click(object sender, RoutedEventArgs e)
{
this.Cursor = Cursors.Wait;
// Dramatic delay...
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
AnswerGenerator generator = new AnswerGenerator();
txtAnswer.Text = generator.GetRandomAnswer(txtQuestion.Text);
this.Cursor = null;
}
<Window x:Class="StackPanel.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">
<StackPanel>
<Label>Stack up buttons</Label>
<Button>Test1</Button>
<Button>Test2</Button>
<Button>Test3</Button>
</StackPanel>
</Window>
<Window x:Class="StackPanel.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">
<StackPanel>
<Label HorizontalAlignment="Center">Stack up buttons</Label>
<Button HorizontalAlignment="Left">Test1</Button>
<Button HorizontalAlignment="Center">Test2</Button>
<Button HorizontalAlignment="Right">Test3</Button>
<Button>Test4</Button>
</StackPanel>
</Window>
<Window x:Class="StackPanel.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">
<StackPanel>
<Label HorizontalAlignment="Center">Stack up buttons</Label>
<Button HorizontalAlignment="Left" Margin="5">Test1</Button>
<Button HorizontalAlignment="Center" Margin="2,5,2,5">Test2</Button>
<Button HorizontalAlignment="Right">Test3</Button>
<Button>Test4</Button>
</StackPanel>
</Window>
public class FrameworkElement: UIElement, ... {
public static readonly DependencyProperty MarginProperty;
...
}
static FrameworkElement() {
FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(
new Thickness(), FrameworkPropertyMetadataOptions.AffectsMeasure);
MarginProperty = DependencyProperty.Register("Margin",
typeof(Thickness), typeof(FrameworkElement), metadata,
new ValidateValueCallback(FrameworkElement.IsMarginValid));
...
}
public Thickness Margin
{
set { SetValue(MarginProperty, value); }
get { return (Thickness)GetValue(MarginProperty); }
}
#include <stdio.h> // # marks a preprocessing directive
Pro WPF 4.5 in C# (4th Edition)
Matthew MacDonald
Thank you for your attention!