Benutzer:MovGP0/WPF/Data Binding

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      

WPF Data Binding

[Bearbeiten | Quelltext bearbeiten]
DataContext defines the source data object (ie. a ViewModel)
Binding binding to a property of the source data object
Change Notifications events that keep UI and model in sync
  • Entity Data Sources
  • Change Notifications
  • EditableObjects and CollectionViews
  • DataSet and XML data sources

Binding Properties

[Bearbeiten | Quelltext bearbeiten]

Bindings can target

  • Any FrameworkElement
  • Any DependencyProperty
Property Usage
Path Source property
XPath XML source node
Source Source object
Mode OneTime, OneWay, OneWayToSource, TwoWay
<TextBox Text="{Binding Path=Property}" />

Binding to Control

[Bearbeiten | Quelltext bearbeiten]
private static readonly DependencyProperty NumeratorProperty = 
    DependencyProperty.Register(nameof(Numerator), typeof(object), typeof(Fraction), new PropertyMetadata(null));

public object Numerator
{
    get { return (object)GetValue(NumeratorProperty); }
    set { SetValue(NumeratorProperty, value); }
}

public Fraction()
{
    InitializeComponent();
    DataContext = this;
}
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:t="clr-namespace:System.Threading;assembly=mscorlib"
      DataSource={Binding}>
  <StackPanel>
  <TextBlock Text="{Binding Path=Numerator}" />
  <Border Height="1" Margin="2" Background="Black" />
  <TextBlock Text="{Binding Path=Denominator}" />
  </StackPanel>
</Grid>

Binding to an static .NET object

[Bearbeiten | Quelltext bearbeiten]
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:t="clr-namespace:System.Threading;assembly=mscorlib">
  <TextBlock Text="{Binding Path=CurrentCulture, Source={x:Static t:Thread.CurrentThread}}" />
</Grid>

Binding to an resource

[Bearbeiten | Quelltext bearbeiten]
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:s="clr-namespace:System;assembly=mscorlib">
  <Grid.Resources>
    <s:String x:Key="myValue">Hello World!</s:String>
  </Grid.Resources>
  <StackPanel>
    <TextBlock Text="{Binding Source={StaticResource myValue}}" />
    <TextBlock Text="{Binding Path=Length, Source={StaticResource myValue}}" />
  </StackPanel>
</Grid>

Binding to an DataContext

[Bearbeiten | Quelltext bearbeiten]
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:s="clr-namespace:System;assembly=mscorlib"
      DataContext="{x:Static s:Environment.OSVersion}">
  <StackPanel>
    <TextBlock Text="{Binding Platform}" />
    <TextBlock Text="{Binding Version}" />
    <TextBlock Text="{Binding ServicePack}" />
  </StackPanel>
</Grid>

Binding to an ViewModel

[Bearbeiten | Quelltext bearbeiten]
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:s="clr-namespace:System;assembly=mscorlib"
      DataContext="{ViewModel}">
  <StackPanel>
    <TextBox Text="{Binding FirstName}" /> <!-- updates ViewModel on LostFocus event -->
    <TextBox Text="{Binding LastName, UpdateSourcheTrigger=PropertyChanged}" /> <!-- updates ViewModel on every property change -->
  </StackPanel>
</Grid>
  • To update the UI when a property of the ViewModel changes, implement the INotifyPropertyChanged interface on the ViewModel.
Single Item
// in constructor 
InitializeComponent();
var person = new Person { ... }; 
DataContext = person;
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:MyNamespace">
  <Window.Resources>
    <DataTemplate DataType="{x:Type local:Person}">
      <Grid DataContext="{ViewModel}">
        <StackPanel>
          <TextBox Text="{Binding FirstName}" /> <!-- updates ViewModel on LostFocus event -->
          <TextBox Text="{Binding LastName, UpdateSourcheTrigger=PropertyChanged}" /> <!-- updates ViewModel on every property change -->
        </StackPanel>
      </Grid>
    </DataTemplate>
  </Window.Resources>
  
  <StackPanel>
    <ContentControl Content="{Binding}" />
  </StackPanel>
</Window>
Item List
// in constructor 
InitializeComponent();
var people = new List<Person> { ... }; 
DataContext = people;
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:MyNamespace">
  <Window.Resources>
    <DataTemplate DataType="{x:Type local:Person}">
      <Grid DataContext="{ViewModel}">
        <StackPanel>
          <TextBox Text="{Binding FirstName}" /> <!-- updates ViewModel on LostFocus event -->
          <TextBox Text="{Binding LastName, UpdateSourcheTrigger=PropertyChanged}" /> <!-- updates ViewModel on every property change -->
        </StackPanel>
      </Grid>
    </DataTemplate>
  </Window.Resources>
  
  <StackPanel>
    <ListBox ItemsSource="{Binding}" />
  </StackPanel>
</Window>