In this tutorial you will learn more core C# and look at styles and data bindings. This will allow you to write a simple C# program that you will then compile. The program will be a Visual Studio Form application
About this Tutorial
Objectives
Delegates will learn to develop applications using C# 4.5. After completing this course, delegates will be able to:
Use Visual Studio 2012 effectively
Create commercial C# Web Applications
Develop multi-threaded applications, Use WCF and LINQ
Audience
This course has been designed primarily for programmers new to the .Net development platform. Delegates experience solely in Windows application development or earlier versions of C# will also find the content beneficial.
Prerequisites
No previous experience in C# programming is required. But any experience you do have in programming will help. Also no experience in Visual Studio is required. But again any experience you do have with programming development environments will be a valuable.
Experience using a contemporary OO language such as C++ or C# would be useful but is not required.
WPF is Microsoft’s new API for creating desktop apps
Utilizes the high-performance DirectX engine.
Much richer interfaces than possible in Windows Forms UIs.
Primary features of WPF:
Web-like layout model.
Rich drawing model for 2-D and 3-D graphics.
Rich text model.
Animation support
Support for audio and video media.
Styles and templates.
Commands.
XAML-based declarative UI.
Interop with Windows Forms controls.
WPF Application Types:
Standalone applications –
XAML browser applications –
WPF User Interfaces and XAML:
You will typically create WPF UIs using XAML:
Extensible Application Markup Language
A declarative syntax for creating objects, and for setting properties and event–handlers
This XAML will create this UI – <Window ... >
...
<Label>Label</Label>
<TextBox>TextBox</TextBox>
<RichTextBox .../>
<RadioButton>RadioButton</RadioButton>
<CheckBox>CheckBox</CheckBox>
<Button>Button</Button>
</Window>
WPF Class Hierarchy –
Estimated Time – 1 Hour
Not what you are looking? Try the next tutorial – Overview of WCF
Lab 1: Creating WPF Applications
Lab 1: Creating WPF Applications
Anatomy of a WPF Standalone Application – Create a new WPF application via a file or new project. The application – .xaml file defines the application; .xaml.cs file is the code-behind file, contains application event-handlers. A Window – .xaml file defines a Window; .xaml.cs file is the code-behind file, contains window event-handlers –
Either add tags to the XAML (manually or from the Toolbox).
Or write C# code to create components programmatically.
XAML is usually the preferred option –
XAML element names (e.g. TextBox) correspond to a WPF class.
Each XAML element creates a new instance of that class. <Window ...>
<Grid>
<TextBox Name="textBox1" .../>
<Button Name="button1">Button</Button>
<Label Name="label1">Label</Label>
</Grid>
</Window>
When you build the application, Visual Studio performs the following tasks – Compiles XAML into BAML; Generates partial classes for the application, and for each window –
To run the application (Ctrl+F5 as usual) –
Lab 2: Styles
Lab 2: Styles
Overview
A style is a collection of property values that can be applied to an element – Similar to the concept of CSS styles in Web applications.
WPF styles allow you to define common characteristic – UI properties, Non-UI properties.
How Styles Work:
WPF elements have a Style property:
You assign a Style object to this property.
The style is automatically inherited by all child elements.
The Style object has a Setters property:
Contains a collection of Setter objects.
Each Setter object specifies:
Property: A dependency property (e.g. Control.FontFamily).
Value: The value to set it to (e.g. “Verdana”).
Lab
Example: Define a one-off style for a Button element – UsingStyles project and SimpleStyle.xaml – <Window x:Class="UsingStyles.SimpleStyle" ...>
<StackPanel>
<Button Name="Button1">
<Button.Style>
<Style>
<Setter Property="Control.FontFamily" Value="Verdana" />
<Setter Property="Control.FontSize" Value="20" />
<Setter Property="Control.HorizontalAlignment" Value="Center" />
</Style>
</Button.Style>
Hello from button 1!
</Button>
</StackPanel>
</Window>
You can define a Setter whose value is a complex object (rather than a simple string) – Set the Setter.Value property to a child element in XAML (or to an object in code).
We’ve given our styles a key name – <Style x:Key="LoudButtonStyle"> ...
… And explicitly applied styles by key name, where we want to – <Button Style="{StaticResource LoudButtonStyle}" ...>
An alternative technique:
Assign styles to a target element type (don’t specify a key name) – <Style x:Key="LoudButtonStyle"> ...
… The style will be applied to all elements of that type by default – <Button ...> Note: Can opt out, via Style="{x:Null}"
Lab 3: Data Binding
Lab 3: Data Binding
Overview
WPF provides very rich support for data binding:
You can create 1-way and 2-way data bindings.
You can bind almost any property on a source object to any dependency property on a target object.
You can bind collections (e.g. a LINQ query) to ListBox etc…
Data Binding Modes – WPF supports four data binding modes –
Lab
Creating a Data Binding:
You will usually create a data binding in XAML – Set the target property to a data binding expression, by using a Binding markup extension. Then specify source element, source property, and mode (e.g. OneWay) – targetProp="{Binding ElementName=srcElem, Path=srcProp, Mode=OneWay}"
Example: element binding:
Simple example of how to bind to an element property: Bind TextBlock FontSize property to Slider Value property.
Define an OnPropertyChanged() method to raise the event.
In each property setter, invoke OnPropertyChanged().
Defining a Class to Support 2-Way Binding.
Example: ObjectBinding project – public class Product : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private int productID;
public int ProductID
{
get
...
set
...
}
...
If you want to bind a collection of objects to a control (e.g. a ListBox); set the ItemsSource property on the UI control – <ListBox Name="ProductsListBox1" />
<ListBox Name="ProductsListBox2" DisplayMemberPath="ProductName" />
<Button Name="GetProductsButton" Content="Get Products" Click="GetProductsButton_Click" />
private void GetProductsButton_Click(object sender, RoutedEventArgs e
{
List<Product> products = "get a collection of products" ;
this.ProductsListBox1.ItemsSource = products;
this.ProductsListBox1.ItemsSource = products;
}
If you liked this post, please comment with your suggestions to help others.
If you would like to see more content like this in the future, please fill-in our quick survey.
Manage cookie consent
You can view this website without consenting to extra cookies. However you will need to accept the 'marketing' cookies to send messages via the contact forms & see any maps displayed on the site
Functional
Always active
Cookies necessary for the website to work.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.Cookies used to track user interaction on the site, which helps us to evaluate & improve the website.
Marketing: Forms & additional content (Marketing)
We need your permission to place ‘marketing’ cookies, so you are able to use the contact forms & see embedded content e.g. videos and maps. - - - - We have added protection to our contact forms to help prove that a human (rather than a spambot) is filling
If you would like to see more content like this in the future, please fill-in our quick survey.