Tutorial – Binding to a member variable object
You have your WPF Window and you have an object that you don’t want to make a static resource. You want to declare it as a member variable in the code.
Example 1 – Binding two TextBox controls to a Person object
- Create a New WPF Application Visual Studio.
- Create a new Class named Person.cs.
- Give it FirstName and a LastName properties.
- Configure it to implement the INotifyPropertyChanged interface.
- Create a NotifyPropertyChanged function that all properties can share (to avoid duplicate code in every single property).
- Configure the properties to call the NotifyPropertyChanged function passing in a string that is the name of the property.
Person.cs
using System; using System.ComponentModel; namespace WPFPerson { public class Person : INotifyPropertyChanged { #region Member Variables String _FirstName; String _LastName; #endregion #region Constructors /* * The default constructor */ public Person() { } #endregion #region Properties public String FirstName { get { return _FirstName; } set { _FirstName = value; NotifyPropertyChanged("FirstName"); } } public String LastName { get { return _LastName; } set { _LastName = value; NotifyPropertyChanged("LastName"); } } #endregion #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } #endregion } }
- Go back tot he MainWindow.xaml.
- Add two labels, and two text boxes, and a button.
- Change the text boxes to be populated using binding by adding the following text:
Text=”{Binding FirstName, Mode=TwoWay}”MainWindow.xaml (WPF Window)
<Window x:Class="WPFPerson.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" > <Grid Name="PersonGrid" > <TextBox Height="23" HorizontalAlignment="Left" Margin="173,87,0,0" Name="textBoxFirstName" VerticalAlignment="Top" Width="234" Text="{Binding FirstName, Mode=TwoWay}" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="173,116,0,0" Name="textBoxLastName" VerticalAlignment="Top" Width="234" Text="{Binding LastName, Mode=TwoWay}"/> <Label Content="FirstName" Height="28" HorizontalAlignment="Left" Margin="103,85,0,0" Name="labelFirstName" VerticalAlignment="Top" /> <Label Content="LastName" Height="28" HorizontalAlignment="Left" Margin="103,114,0,0" Name="labelLastName" VerticalAlignment="Top" /> <Button Content="Defaults" Height="23" HorizontalAlignment="Left" Margin="337,199,0,0" Name="buttonDefaults" VerticalAlignment="Top" Width="75" Click="buttonDefaults_Click" /> </Grid> </Window>
- Double-click the button to create the buttonDefaults_Click event function.
This also conveniently takes you to the Code Behind of the MainWindow.cs file. - Have the buttonDefaults_Click function update to properties of your _Person object.
_Person.FirstName = “Jared”;
_Person.LastName = “Barneck”; - Create a field/member variable using the Person object.
private readonly Person _Person; - Now in the constructor initialize the object.
_Person = new Person(); - Also in the constructor, make the DataContext for each TextBox the _Person object.
textBoxFirstName.DataContext = _Person;
textBoxLastName.DataContext = _Person;MainWindow.cs (Code Behind)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading; namespace WPFPerson { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private readonly Person _Person; public MainWindow() { _Person = new Person(); InitializeComponent(); textBoxFirstName.DataContext = _Person; textBoxLastName.DataContext = _Person; } private void buttonDefaults_Click(object sender, RoutedEventArgs e) { _Person.FirstName = "Jared"; _Person.LastName = "Barneck"; } } }
- Now Now compile and make sure you don’t have any errors.
Example 2 – Forthcoming…
Example 3 – Forthcoming…
Sources:
http://www.wrox.com/WileyCDA/Section/Windows-Presentation-Foundation-WPF-Data-Binding-with-C-2005.id-305562.html