Binding FontFamily / FontSize value for current Control





Bind to a Collection with the Master-Detail Pattern





Bind to IDataErrorInfo







//File:Window.xaml.cs
using System.Windows;
using System.ComponentModel;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = new Employee(){FirstName = “A”,Age = 26,};
}
}

public class Employee : INotifyPropertyChanged,IDataErrorInfo
{
private string firstName;
private int age;

public Employee()
{
FirstName = “A”;
}
public string FirstName
{
get
{
return firstName;
}
set
{
if(firstName != value)
{
firstName = value;
OnPropertyChanged(“FirstName”);
}
}
}

public int Age
{
get
{
return age;
}
set
{
if(this.age != value)
{
this.age = value;
OnPropertyChanged(“Age”);
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string propertyName){
if(this.PropertyChanged != null)
{
this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}
public string Error
{
get
{
return string.Empty;
}
}

public string this[string propertyName]
{
get
{
string message = string.Empty;

switch(propertyName)
{
case “FirstName”:
if(string.IsNullOrEmpty(firstName))
message = “A person must have a first name.”;
break;

case “Age”:
if(age < 1) message = "A person must have an age."; break; default: break; } return message; } } } } [/csharp]




Bind to a collection





DataTemplate for binding





Binding Environment Info





Bind to an ADO.NETDataSet























//File:Window.xaml.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Collections.Generic;

namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
this.InitializeComponent();
}
DataSet myDataSet;
private void OnInit(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:BookData.mdb”);
OleDbDataAdapter adapter = new OleDbDataAdapter(“SELECT * FROM BookTable;”, conn);

myDataSet = new DataSet();
adapter.Fill(myDataSet, “BookTable”);

myListBox.DataContext = myDataSet;
}
private void OnClick(object sender, RoutedEventArgs e)
{
DataTable myDataTable = myDataSet.Tables[“BookTable”];
DataRow row = myDataTable.NewRow();

row[“Title”] = “A”;
row[“ISBN”] = “0-1111-1111-2”;
row[“NumPages”] = 1;
myDataTable.Rows.Add(row);
}
}
public class IntColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int numValue = (int)value;
if (numValue < 50) return System.Windows.Media.Brushes.Green; else return System.Windows.Media.Brushes.Red; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } } } [/csharp]