Does anyone know of a comprehensive collection of WPF binding examples?

It seems that every time I read an article on how to make WPF data binding, it is done with some different options, sometimes with a DataContext, sometimes without, sometimes with Itemssource or with ItemSource and DataContext, there is also an ObjectDataProvider, and you can have any of them in XAML or code, or without code, and link directly from XAML to your ViewModels.

There seem to be dozens of different syntaxes to use in XAML itself, for example:

<ListBox ItemsSource="{Binding Source={StaticResource Customers}}">

and

<ListBox DataContext="{StaticResource Customers}" ItemsSource="{Binding}">

These two code examples, for example, do the same:

1. Using ObjectDataProvider without code:

<Window x:Class="TestDataTemplate124.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestDataTemplate124"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="Customers"
                            ObjectType="{x:Type local:Customer}"
                            MethodName="GetAllCustomers"/>
    </Window.Resources>
    <StackPanel>
        <ListBox DataContext="{StaticResource Customers}" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}"/>
                        <TextBlock Text=" "/>
                        <TextBlock Text="{Binding LastName}"/>
                        <TextBlock Text=" ("/>
                        <TextBlock Text="{Binding Age}"/>
                        <TextBlock Text=")"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

2. An example without a DataContext:

<Window x:Class="TestDataTemplate123.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestDataTemplate123"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ListBox x:Name="ListBox1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}"/>
                        <TextBlock Text=" "/>
                        <TextBlock Text="{Binding LastName}"/>
                        <TextBlock Text=" ("/>
                        <TextBlock Text="{Binding Age}"/>
                        <TextBlock Text=")"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

    using System.Collections.ObjectModel;
    using System.Windows;

    namespace TestDataTemplate123
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                ListBox1.ItemsSource = Customer.GetAllCustomers();
            }
        }
    }

- , WPF Databinding, , ", ", , , , , DataContext , XAML ..?

+5
2

this cheatsheet

+3

Bea Stollnitz. , Microsoft WPF, . WPF, . .

+1

All Articles