I\'m working on some application and I have one problem. I have two windows (Bookings - parent and Guests - child). In the parent window, I have one combo box with list of guest
A simple callback interface can help you. The client should subscribe and if server pushes a notification then either populate the combobox or refresh.
Here is a tutorial: http://idunno.org/archive/2008/05/29/wcf-callbacks-a-beginners-guide.aspx
I used to created a "login" method and store every client callback on server side. Then whenever I needed to use push notification I simply used this stored client-callback. On the client side the you can handle the recieved message / event as you want.
Hold BookingsViewModel
instance in GuestViewModel
class and call BookingsViewModel.OnPropertyChanged("Guest")
when adding a new Guest (after wcf.AddGuest(Guest);
line).
If I understand problem correctly - you want to get a new guest from child window and update your collection in parent window - then there are many ways to do it.
For example:
Add event "OnSuccessful" to your GuestsViewModel and then attach BookingsViewModel.
Implement EventAggregator pattern (subscriber - listener)
Just refresh guests after closing child window (show it as a modal dialog).
If your creating your child window from your main window as well as your child view model, you could either look at your window closed event or if you need to know before the window gets closed you could also subscribe to the property changed event of the child window when it is created an when a property changes on the child view model you can check to see if its the property you want. If so update your list.
ChildViewModel n = new ChildViewModel()
n.PropertyChanged += new PropertyChangedEventHandler(n_PropertyChanged);
void n_PropertyChanged(object sender, PropertyChangedEventArgs e)
{ if(e.PropertyName == "myChangingObject")
//reload list
}
OK, I added this lines in my "GuestsViewModel.cs":
private BookingsViewModel _BookingsViewModel;
public BookingsViewModel BookViewModel // Property
{
get
{
return _BookingsViewModel;
}
set
{
_BookingsViewModel = value;
OnPropertyChanged("BookViewModel");
}
}
public GuestsViewModel(BookingsViewModel bvm) // Constructor with one parameter
{
BookViewModel = bvm;
}
public ICommand _btnAddGuest;
public ICommand btnAddGuest
{
get
{
if (_btnAddGuest == null)
{
_btnAddGuest = new DelegateCommand(delegate()
{
try
{
Service1Client wcf = new Service1Client();
wcf.AddGuest(Guest);
BookingsViewModel.OnPropertyChanged("Guests"); // You said to add this
wcf.Close();
}
catch
{
Trace.WriteLine("working...", "MyApp");
}
});
}
return _btnAddGuest;
}
}
Did you mean something like this above? This don't work... I suppose I should add one more thing? Sorry, maybe this questions are stupid but I don't have idea how to solve this problem...
Regards, Vladimir
Just use your already existent connection to your GuestViewModel
from your BookingsViewModel
.
The following suggestion is not tested but you will get the idea
public ICommand btnAddGuest // Command for opening child window
{
get
{
if (_btnAddGuest == null)
{
_btnAddGuest = new DelegateCommand(delegate()
{
try
{
Guests guest = new Guests();
guest.ShowDialog();
// Add some Logic here and an is save check property to your GuestVM
// sample solution
// var vm = guest.DataContext as GuestViewModel;
// if(vm != null)
// if(vm.IsSaved)
// {
// var model = vm.Guest as tblGuest;
// Guests.Add(model); // will add him to your list
// Guest = model // will add him at your selected Guest
// }
}
catch
{
Trace.WriteLine("working...", "MyApp");
}
});
}
return _btnAddGuest;
}
}