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.
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 –
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).
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.