Binding Property with Exception


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BindingWithFallback" Height="300" Width="300">
    <StackPanel>
      <TextBox Text="{Binding Path=ExceptionProperty, Mode=OneWay, FallbackValue=(oops)}" IsReadOnly="True" />
    </StackPanel>
</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 WpfApplication1{

  class MyClass {
    public string ExceptionProperty { get { throw new Exception("Oops!"); } }
  }

  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
      DataContext = new MyClass();
    }

  }
}

   
    
     


Null property binding


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BindingWithFallback" Height="300" Width="300">
    <StackPanel>
      <TextBox Text="{Binding Path=NullProperty.FastProperty, Mode=OneWay, FallbackValue=(null)}" IsReadOnly="True" />
    </StackPanel>
</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 WpfApplication1{

  class MyClass {
    public MyClass NullProperty { get { return null; } }
  }

  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
      DataContext = new MyClass();
    }

  }
}

   
    
     


Async binding


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BindingWithFallback" Height="300" Width="300">
    <StackPanel>
      <TextBox Text="{Binding Path=MyProperty, Mode=OneWay, FallbackValue=(waiting), IsAsync=True}" IsReadOnly="True" />
    </StackPanel>
</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 WpfApplication1{

  class MyClass {
    public string MyProperty { get { System.Threading.Thread.Sleep(2000); return "BBB"; } }
  }

  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
      DataContext = new MyClass();
    }

  }
}

   
    
     


List Binding


   
  

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfApplication1"
  Title="List Binding" Height="135" Width="315">
  <Window.Resources>
    <local:People x:Key="Family">
      <local:Person Name="A" Age="11" />
      <local:Person Name="B" Age="27" />
      <local:Person Name="C" Age="38" />
    </local:People>
    <local:AgeToForegroundConverter x:Key="ageConverter" />
  </Window.Resources>
  <Grid DataContext="{StaticResource Family}">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="0" Margin="5" VerticalAlignment="Center">Name:</TextBlock>
    <TextBox Grid.Row="0" Grid.Column="1" Margin="5" Text="{Binding Path=Name}" />
    <TextBlock Grid.Row="1" Grid.Column="0" Margin="5" VerticalAlignment="Center">Age:</TextBlock>
    <TextBox Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding Path=Age}"
             Foreground="{Binding Path=Age, Converter={StaticResource ageConverter}}" />
    <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal">
      <Button Name="birthdayButton" Width="75" Margin="5">Birthday</Button>
      <Button Name="backButton" Width="75" Margin="5">&amp;lt;</Button>
      <Button Name="forwardButton" Width="75" Margin="5">&amp;gt;</Button>
    </StackPanel>
  </Grid>
</Window>

//File:Window.xaml.cs


using System;
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;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;

namespace WpfApplication1 {
  public class Person : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propName) {
      if( this.PropertyChanged != null ) {
        PropertyChanged(this, new PropertyChangedEventArgs(propName));
      }
    }

    string name;
    public string Name {
      get { return this.name; }
      set {
        if( this.name == value ) { return; }
        this.name = value;
        Notify("Name");
      }
    }

    int age;
    public int Age {
      get { return this.age; }
      set {
        if( this.age == value ) { return; }
        this.age = value;
        Notify("Age");
      }
    }

    public Person() { }
    public Person(string name, int age) {
      this.name = name;
      this.age = age;
    }
  }

  class People : List<Person> { }
  public partial class Window1 : Window {
    public Window1() {
      InitializeComponent();
      this.birthdayButton.Click += birthdayButton_Click;
      this.backButton.Click += backButton_Click;
      this.forwardButton.Click += forwardButton_Click;
    }

    ICollectionView GetFamilyView() {
      People people = (People)this.FindResource("Family");
      return CollectionViewSource.GetDefaultView(people);
    }

    void birthdayButton_Click(object sender, RoutedEventArgs e) {
      ICollectionView view = GetFamilyView();
      Person person = (Person)view.CurrentItem;

      ++person.Age;
      System.Console.WriteLine(person.Name);
      System.Console.WriteLine(person.Age);
    }

    void backButton_Click(object sender, RoutedEventArgs e) {
      ICollectionView view = GetFamilyView();
      view.MoveCurrentToPrevious();
      if( view.IsCurrentBeforeFirst ) {
        view.MoveCurrentToFirst();
      }
    }

    void forwardButton_Click(object sender, RoutedEventArgs e) {
      ICollectionView view = GetFamilyView();
      view.MoveCurrentToNext();
      if( view.IsCurrentAfterLast ) {
        view.MoveCurrentToLast();
      }
    }

  }

  public class AgeToForegroundConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      Debug.Assert(targetType == typeof(Brush));
      int age = int.Parse(value.ToString());
      return (age > 25 ? Brushes.Red : Brushes.Black);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      throw new NotImplementedException();
    }
  }

}

   
    
     


Text Data Binding


   
  
<Window x:Class="TextDataBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TextDataBinding" Height="300" Width="300">
    <Grid>
      <TextBlock>
        Name:
        <TextBlock Text="{Binding FirstName}" />
        <TextBlock Text="{Binding LastName}" />
      </TextBlock>
    </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 TextDataBinding
{
    public partial class Window1 : System.Windows.Window
    {
        Person src = new Person();
        public Window1()
        {
            InitializeComponent();

            src.FirstName = "A";
            src.LastName = "B";

            this.DataContext = src;
        }

    }
    public class Person
    {
        private string firstNameValue;

        public string FirstName
        {
            get { return firstNameValue; }
            set { firstNameValue = value; }
        }

        private string lastNameValue;

        public string LastName
        {
            get { return lastNameValue; }
            set { lastNameValue = value; }
        }

    }
}

   
    
     


Data binding using collections composed of mixed types of data.


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:WpfApplication1"
  x:Class="WpfApplication1.Window1"
  Title="CompositeCollections"
  SizeToContent="WidthAndHeight">
  <Window.Resources>
    <c:Employees x:Key="EmployeesData"/>

    <XmlDataProvider x:Key="NewStudentesData" XPath="NewStudentes/Student">
      <x:XData>
      <NewStudentes xmlns="">
        <Student Name="Jason" />
        <Student Name="Hercules" />
        <Student Name="Bellerophon" />
        <Student Name="Theseus" />
        <Student Name="Odysseus" />
        <Student Name="Perseus" />
      </NewStudentes>
      </x:XData>
    </XmlDataProvider>

    <DataTemplate DataType="{x:Type c:Employee}">
      <TextBlock Text="{Binding Path=Name}" Foreground="Gold"/>
    </DataTemplate>
    <DataTemplate DataType="Student">
      <TextBlock Text="{Binding XPath=@Name}" Foreground="Cyan"/>
    </DataTemplate>
  </Window.Resources>
  
  <StackPanel>
    <ListBox Name="myListBox" Height="300" Width="200">
      <ListBox.ItemsSource>
        <CompositeCollection>
          <CollectionContainer Collection="{Binding Source={StaticResource EmployeesData}}" />
          <CollectionContainer Collection="{Binding Source={StaticResource NewStudentesData}}" />
          <ListBoxItem Foreground="Red">Other Listbox Item 1</ListBoxItem>
        </CompositeCollection>
      </ListBox.ItemsSource>
    </ListBox>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }
  }

  public class Employee
  {
        private string _name;

    public string Name
    {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
    }
        
        public Employee(string name)
        {
            Name = name;
        }
  }

  public class Employees : ObservableCollection<Employee>
  {
    public Employees()
    {
      Add(new Employee("A"));
      Add(new Employee("B"));
      Add(new Employee("C"));
    }
  }
}

   
    
     


Master Detail Binding


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MasterDetailBinding"
    xmlns:local="clr-namespace:WpfApplication1">
  <Window.Resources>
    <local:Companies x:Key="Companies">
      <local:Company CompanyName="Stooge">
        <local:Company.Members>
          <local:Team>
            <local:Employee Name="Larry" Age="21">
              <local:Employee.Skills>
                <local:Skills>
                  <local:Skill Description="A" />
                  <local:Skill Description="B" />
                  <local:Skill Description="C" />
                </local:Skills>
              </local:Employee.Skills>
            </local:Employee>
            <local:Employee Name="Moe" Age="22" >
              <local:Employee.Skills>
                <local:Skills>
                  <local:Skill Description="D" />
                  <local:Skill Description="E" />
                </local:Skills>
              </local:Employee.Skills>
            </local:Employee>
            <local:Employee Name="Curly" Age="23" >
              <local:Employee.Skills>
                <local:Skills>
                  <local:Skill Description="F" />
                  <local:Skill Description="G" />
                </local:Skills>
              </local:Employee.Skills>
            </local:Employee>
          </local:Team>
        </local:Company.Members>
      </local:Company>
      <local:Company CompanyName="Addams">
        <local:Company.Members>
          <local:Team>
            <local:Employee Name="Gomez" Age="135" >
              <local:Employee.Skills>
                <local:Skills>
                  <local:Skill Description="H" />
                  <local:Skill Description="Z" />
                  <local:Skill Description="Q" />
                </local:Skills>
              </local:Employee.Skills>
            </local:Employee>
            <local:Employee Name="Morticia" Age="121" >
              <local:Employee.Skills>
                <local:Skills>
                  <local:Skill Description="R" />
                  <local:Skill Description="P" />
                  <local:Skill Description="L" />
                </local:Skills>
              </local:Employee.Skills>
            </local:Employee>
            <local:Employee Name="Fester" Age="137" >
              <local:Employee.Skills>
                <local:Skills>
                  <local:Skill Description="R" />
                  <local:Skill Description="S" />
                  <local:Skill Description="U" />
                </local:Skills>
              </local:Employee.Skills>
            </local:Employee>
          </local:Team>
        </local:Company.Members>
      </local:Company>
    </local:Companies>
  </Window.Resources>
  <Grid DataContext="{StaticResource Companies}">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="0">Companies:</TextBlock>
    <ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Path=CompanyName}" />
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
      <TextBlock Text="{Binding Path=CompanyName}" />
      <TextBlock Text=" Company Members:" />
    </StackPanel>
    <ListBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Path=Members}" IsSynchronizedWithCurrentItem="True">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock>
            <TextBlock Text="{Binding Path=Name}" />
            (age: <TextBlock Text="{Binding Path=Age}" />)
          </TextBlock>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    <StackPanel Grid.Row="0" Grid.Column="2" Orientation="Horizontal">
      <TextBlock Text="{Binding Path=Members/Name}" />
      <TextBlock Text=" Skills:" />
    </StackPanel>
    <ListBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding Path=Members/Skills}" IsSynchronizedWithCurrentItem="True">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Path=Description}" />
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>

  </Grid>
</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;
using System.Collections.ObjectModel;


namespace WpfApplication1 {

  public class Skill {
    string description;
    public string Description {
      get { return description; }
      set { description = value; }
    }
  }

  public class Skills : ObservableCollection<Skill> { }

  public class Employee {
    string name;
    public string Name {
      get { return name; }
      set { name = value; }
    }

    int age;
    public int Age {
      get { return age; }
      set { age = value; }
    }

    Skills traits;
    public Skills Skills {
      get { return traits; }
      set { traits = value; }
    }
  }

  public class Team : ObservableCollection<Employee> { }

  public class Company {
    string familyName;
    public string CompanyName {
      get { return familyName; }
      set { familyName = value; }
    }

    Team members;
    public Team Members {
      get { return members; }
      set { members = value; }
    }
  }

  public class Companies : ObservableCollection<Company> { }

  public partial class Window1 : Window {
    public Window1() {
      InitializeComponent();
    }
  }
}