Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Data Binding works through properties, so you won't use any of the member variables in your Binding, eg:</p> <pre><code>int _first public int First { get { return _first; } } </code></pre> <p>you'll use First and not _first for the binding. Normally I've seen each class provide it's own properties to bind to, in which case you could modify your code to:</p> <pre><code>class One : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } int _first = 1; int _second = 2; public int First { get { return _first; } set { _first = value; OnPropertyChanged("First"); } } public int Second { get { return _second; } set { _second = value; OnPropertyChanged("Second"); } } } class Two : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } string _third = "Third"; string _fourth = "Fourth"; public string Third { get { return _third; } set { _third = value; OnPropertyChanged("Third"); } } public string Fourth { get { return _fourth; } set { _fourth = value; OnPropertyChanged("Fourth"); } } } class Comp : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } int _int1 = 100; One _part1 = new One(); Two _part2 = new Two(); public One Part1 { get { return _part1; } set { _part1 = value; OnPropertyChanged("Part1"); } } public Two Part2 { get { return _part2; } set { _part2 = value; OnPropertyChanged("Part2"); } } public int Int1 { get { return _int1; } set { _int1 = value; OnPropertyChanged("Int1"); } } } </code></pre> <p>Note that I made the properties public, while keeping the fields defaulted to private. If you assign the DataContext of a parent control to an instance of Comp:</p> <pre><code>Comp comp = new Comp(); stack.DataContext = comp; </code></pre> <p>You can then bind to the pieces in xaml as follows:</p> <pre><code>&lt;Window x:Class="TestApp.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestApp" Title="Window1" Height="300" Width="300"&gt; &lt;StackPanel Name="stack"&gt; &lt;TextBlock Text="{Binding Int1}"/&gt; &lt;TextBlock Text="{Binding Part1.First}"/&gt; &lt;TextBlock Text="{Binding Part1.Second}"/&gt; &lt;TextBlock Text="{Binding Part2.Third}"/&gt; &lt;TextBlock Text="{Binding Part2.Fourth}"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>here you'll see the StackPanel is given a Comp as the DataContext (and therefore all of it's children also have that DataContext unless another one is specified), and the text is bound to it's member classes.</p> <p>EDIT: I also added in setters as well as implemented INotifyPropertyChanged which is a vital component to databinding. With this implemented, you'll be able to bind your data to multiple controls and see the data update in all controls when updated. You'll need to add: using System.ComponentModel;</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload