WPF data binding not updating

Having issues with data binding

Page 1 of 1

2 Replies - 11335 Views - Last Post: 15 January 2009 - 05:52 AM Rate Topic: -----

#1 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

WPF data binding not updating

Post icon  Posted 15 January 2009 - 03:21 AM

Hi there, having a few issues with data binding not seeming to update when a property updates. I'm setting the binding in code, and have trawled the net but can't find any reason why my code won't work! Please help I'm stuck in a rut!

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.Timers;

namespace bindingTest
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		// object I want to use as source
		NumGeneratorTestClass sourceObject = new NumGeneratorTestClass();

		public Window1()
		{
			InitializeComponent();
		}

		// in initialisation of object I wanna set binding
		private void label1_Initialized(object sender, EventArgs e)
		{
			// create new binding
			Binding bindingElement = new Binding();
			// set source
			bindingElement.Source = this.sourceObject;
			// set path
			bindingElement.Path = new PropertyPath("MyNum");
			// set mode
			bindingElement.Mode = BindingMode.OneWay;
			//set update to when property changes
			bindingElement.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
			// lastly set binding to label
			label1.SetBinding(Label.ContentProperty, bindingElement);
		}
	}

	/// <summary>
	/// Just a test class that I can use as an example
	/// </summary>
	public class NumGeneratorTestClass
	{
		/// <summary>
		/// The number I want to bind to
		/// </summary>
		public Int32 MyNum = 23;

		/// <summary>
		/// Constructor
		/// </summary>
		public NumGeneratorTestClass()
		{
			// create new timer
			Timer incrementTimer = new Timer();
			// set interval
			incrementTimer.Interval = 1000;
			// set up event handler for elapsed
			incrementTimer.Elapsed += new ElapsedEventHandler(incrementTimer_Elapsed);
			// start timer
			incrementTimer.Start();
		}

		void incrementTimer_Elapsed(object sender, ElapsedEventArgs e)
		{
			// set random number
			this.MyNum = new Random().Next(0,100);
		}
	}
}



And the simple WPF is...
<Window x:Class="bindingTest.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="Window1" Height="300" Width="300">
	<Grid>
		<Label Margin="93,119,65,115" Name="label1" Initialized="label1_Initialized"></Label>
	</Grid>
</Window>



Please point me in the right direction :)

Is This A Good Question/Topic? 0
  • +

Replies To: WPF data binding not updating

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: WPF data binding not updating

Posted 15 January 2009 - 05:29 AM

I think I figured this out... does this look ok?

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.Timers;
using System.ComponentModel;

namespace bindingTest
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		// object I want to use as source
		NumGeneratorTestClass sourceObject = new NumGeneratorTestClass();

		public Window1()
		{
			InitializeComponent();
		}

		// in initialisation of object I wanna set binding
		private void label1_Initialized(object sender, EventArgs e)
		{
			// create new binding
			Binding bindingElement = new Binding();
			// set source
			bindingElement.Source = this.sourceObject;
			// set path
			bindingElement.Path = new PropertyPath("MyNum");
			// set mode
			bindingElement.Mode = BindingMode.OneWay;
			//set update to when property changes
			bindingElement.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
			// lastly set binding to label
			label1.SetBinding(Label.ContentProperty, bindingElement);
		}
	}

	/// <summary>
	/// Just a test class that I can use as an example
	/// </summary>
	public class NumGeneratorTestClass : INotifyPropertyChanged
	{
		public event PropertyChangedEventHandler PropertyChanged;

		/// <summary>
		/// The number I want to bind to
		/// </summary>
		public Int32 MyNum
		{
			get { return myNum; }
			set
			{
				myNum = value;
				onpropertychanged("MyNum");
			}
		}

		private Int32 myNum;

		/// <summary>
		/// Constructor
		/// </summary>
		public NumGeneratorTestClass()
		{
			// create new timer
			Timer incrementTimer = new Timer();
			// set interval
			incrementTimer.Interval = 1000;
			// set up event handler for elapsed
			incrementTimer.Elapsed += new ElapsedEventHandler(incrementTimer_Elapsed);
			// start timer
			incrementTimer.Start();
		}

		void incrementTimer_Elapsed(object sender, ElapsedEventArgs e)
		{
			// set random number
			this.MyNum = new Random().Next(0,100);
		}

		private void onpropertychanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}



Basically I needed to implement the INotifyPropertyChanged Interface!
Was This Post Helpful? 0
  • +
  • -

#3 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: WPF data binding not updating

Posted 15 January 2009 - 05:52 AM

So the above works for an example, but in practice I have lots of classes, some of which have static properties, and some of the classes are static. I'm having trouble binding to them, as the above method cannot be used as you cannot use interfaces with static classes
Was This Post Helpful? 1

Page 1 of 1