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
Hmm it seems like your website ate my first comment (it was super long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.
I too am an aspiring blog writer but I’m still new to everything.
Do you have any suggestions for novice blog writers? I’d genuinely appreciate it.
Thanks for this examples. All I have found on MSDN are examples with an XML data source, which is not helpful for what we are doing.
One thing I found is that if all of your bindings are coming from the same object, you can set DataContext in the code behind object once rather than having to set it for each field. For example,
DataContext = _Person;
would work in step 14 above. Of course, if you want to bind to multiple objects this does not work…
Glad to have provided an example.
I think there was a reason that I did the binding that way, maybe for what I was going to write in Example 2/3 but I never got back to finished example 2 and 3.