Abhijeet
Abhijeet

Reputation: 13906

Reload a WPF Window for Consistent UI?

I have been an experienced programmer on Asp.Net, where if on-screen data binding changes I could do reload the web-page.

In WPF application: MainWindow I have displayed records in comboxes. User clicks Add New, I open a new WPF window to add a tuple into DB and returns back to MainWindow.

But data bindings on MainPage are out-dated.

Should I manually rebind the data for MainWindow Controls?

Is there a way in WPF to rebind all controls on the page?

Upvotes: 0

Views: 2411

Answers (2)

kenjara
kenjara

Reputation: 402

If you do not have a proper binding set up then you may need to refresh the items in the combo box manually after adding something to the collection.

combobox.Items.Refresh()

You should look into MVVM and INotifyPropertyChanged as mentioned above though.

Upvotes: 0

Gauthier
Gauthier

Reputation: 1331

Proper binding should take care of those issues for you. Nothing should have to be reloaded. Mainwindow

 ObservableCollection<SomeClass> _mainscreenlist = new ObservableCollection<SomeClass>();
 // populate _mainscreenlist with your objects

 void OpenNewWindow() {
  var window = new YourOtherWindow(_mainscreenlist);
  window.Show();
 }

YourOtherWindow

private ObservableCollection<SomeClass> _passedlist;
// 
public YourOtherWindow(ObservableCollection<SomeClass> passedlist) {
 _passedlist = passedlist;
}

private void YourAddMethod() {
    // sadly this doesnt work so will with tuples
    // you will need SomeClass to be a class you create an instance of
    var x = new SomeClass();
    // set data on x as appropiate

    // this triggers wpf to update the mainwindow list, without reloading the window
    _passedlist.add(x);
}

Xaruth mention INotifyPropertyChanged which has a good example INotifyPropertyChanged

Your SomeClass should implement INotifyPropertyChanged so later if one of your items changes the binding will update the changes on screen.

Upvotes: 1

Related Questions