views:

346

answers:

2

It seems that every time I read an article on "how to do WPF data binding", it is done with some different variation, sometimes with DataContext, sometimes without, sometimes with Itemssource or both Itemssource and DataContext, there's also ObjectDataProvider, and you can have any of these in XAML or codebehind, or no codebehind and bind directly from XAML to your ViewModels.

It seems like there are dozens of different syntaxes to use within the XAML itself, e.g.:

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

and

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

These two code samples, for example, do the same thing:

1. Using ObjectDataProvider with no code-behind:

<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. Example with no 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();
            }
        }
    }

Does anyone know of a source that explains WPF Databinding by instead of just saying "here's how you do databinding" and then explain one particular way, but instead attempt to explain the various ways to go about databinding and show perhaps what the advantages and disadvantages of e.g. having DataContext or not, binding in XAML or code-behind, etc.?

+1  A: 

I can recommend you the blog from Bea Stollnitz. If I'm not mistaken, she works at Microsoft and is involved in the development on WPF, particularly in databinding. She really has some great WPF tutorials, many on databinding. You should find some really good info here.

Razzie
+3  A: 

Check out this cheatsheet

Entrodus