The secret sauce behind {binding} in xaml

12
The Secret Sauce Behind { Binding } in XAML Brendon Page

Transcript of The secret sauce behind {binding} in xaml

Page 1: The secret sauce behind {binding} in xaml

The Secret Sauce Behind {Binding} in XAML

Brendon Page

Page 2: The secret sauce behind {binding} in xaml

{Overview}

• What is binding• Why is it difficult• Adding some Context• Behind the scenes

Page 3: The secret sauce behind {binding} in xaml

{What Is It}

DataUI

textBox.Text = Value;

Value = textBox.Text;

<TextBox Text="{Binding}" />

• Mechanism for interacting with data.• Display, convert, update data

Page 4: The secret sauce behind {binding} in xaml

{Quick Demo}

Page 5: The secret sauce behind {binding} in xaml

{Difficult}

• There’s a lot of other things you need to know– DependencyObject /DependencyProperty– INotifyPropertyChanged / INotifyCollectionChanged– IValueConverter

• Cryptic debugging feedback• It’s abstract– MSDN (WPF) Overview

• More than 7500 words• 12 Images• 20 Code blocks

Page 6: The secret sauce behind {binding} in xaml

{A lot to know}

Page 7: The secret sauce behind {binding} in xaml

• All errors are shown in the output window• When data source is set

– System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''Person' (HashCode=46763000)'. BindingExpression:Path=Name; DataItem='Person' (HashCode=46763000); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

• When data source isn’t set–

{Debugging}

Page 8: The secret sauce behind {binding} in xaml

{Quick Demo}

Page 9: The secret sauce behind {binding} in xaml

{Context}

<Window> <TextBox Text="{Binding Path=Name}" /></Window >

public sealed partial class MainWindow : Window{ public MainPage() { DataContext = new Person { Name = "Brendon" }; }}

XAML

C# DataContextBinding Uses

As it’s default datasource

Cascades down

Page 10: The secret sauce behind {binding} in xaml

{Just an Object}

<TextBox Text="{Binding Path=Name}" />

XAML

TextBox textBox = new TextBox();

C#

Binding binding = new Binding("Name");

textBox.SetBinding(TextBox.TextProperty, binding);

textBox.Text = binding;

Page 11: The secret sauce behind {binding} in xaml

Design Time

{But Wait There’s More}

Runtime

• Does the work• Read & writes data between

source & target• Subscribes to property changed events• Maintains the relationship

• Describes the binding,Source, Target, DataItem,Path, Mode

Page 12: The secret sauce behind {binding} in xaml

{Conclusion}

• Interact with data• Binding is tricky, until you understand it• Context is important• It’s just an object