DataBinding, refresh data if DataContext changes?

You're not changing the DataContext . You're changing the value in the property that you set the DataContext to.

Up vote 1 down vote favorite share g+ share fb share tw.

My code: XAML: Customer class: public class Customer { public string FirstName { get; set; } public string LastName { get; set; } } Code behind: public partial class MainWindow : Window, INotifyPropertyChanged { private int index = 0; public Customer Tmp; List ar = new List(); public MainWindow() { InitializeComponent(); ar. Add(new Customer { FirstName = "qwe", LastName = "rty" }); ar. Add(new Customer { FirstName = "asd", LastName = "asd" }); this.

Tmp = arindex; this. DataContext = this. Tmp; } private void button1_Click(object sender, RoutedEventArgs e) { this.

Tmp = ar++index; if (this. PropertyChanged! = null) { this.

PropertyChanged(this, new PropertyChangedEventArgs("Tmp")); } } public event PropertyChangedEventHandler PropertyChanged; } So when application loads, everything is fine - textbox shows "qwe", but button, which should load second customer object doesn't work. What am I doing wrong? C# wpf xaml data-binding link|improve this question edited Jul 24 '11 at 17:55H.B.43k61645 asked Jul 24 '11 at 17:36bah245111 89% accept rate.

You're not changing the DataContext. You're changing the value in the property that you set the DataContext to. You don't need a Tmp property at all.

Just change the DataContext in your event handler, e.g. : DataContext = ar++index.

– bah Jul 24 '11 at 17:46 @bah - No, nothing you have is "bound to" Tmp, so changing it would not affect the UI. When do you do this. DataContext = this.

Tmp that sets DataContext to the same reference as Tmp (which is qwe). So changing Tmp won't affect the DataContext. – CodeNaked Jul 24 '11 at 17:53 Ah well, this will do for now, it seems there are a lot of things for me to learn :) – bah Jul 24 '11 at 17:59.

You would need to make Tmp a property and then bind DataContext to it, like this; private Customer tmp; public Customer Tmp { get { return this. Tmp; } get { this. Tmp = value; if (this.

PropertyChanged! = null) this. PropertyChanged(this, new PropertyChangedEventArgs("Tmp")); } } public MainWindow() { InitializeComponent(); ar.

Add(new Customer { FirstName = "qwe", LastName = "rty" }); ar. Add(new Customer { FirstName = "asd", LastName = "asd" }); this. Tmp = arindex; this.

SetBinding(DataContextProperty, new Binding("Tmp") { Source = this }); }.

– bah Jul 24 '11 at 17:43 @bah - You only set DataContext once, so it would never update. By binding it to your Tmp property, then anytime you change Tmp the DataContext will also change. – CodeNaked Jul 24 '11 at 17:45.

You would need to make Tmp a property and then bind DataContext to it, like this.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions