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 guests and one button for adding new guest. When I click on that button Guests window (child window) opens. In the child window I am adding new guest into database and that works fine. My question is: How to refresh/update combo box list in parent window after adding a new guest in the child window? I know that changes in the property should be reflected in the view without retrieving data from database (thanks to binding).
Bookings.xaml
<ComboBox ItemsSource="{Binding Path=Guests}" SelectedItem="{Binding Path=Guest}" Height="25" HorizontalAlignment="Left" IsEditable="True" IsTextSearchEnabled="True" Margin="119,10,0,0" Name="cbGuest" Padding="3,1,1,1" TextSearch.TextPath="Name" VerticalAlignment="Top" VerticalContentAlignment="Center" Width="141" FontFamily="Times New Roman" FontWeight="Bold" FontSize="14">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}" Text="{MultiBinding StringFormat='\{0\} ', Bindings={Binding Path=Name}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button BorderBrush="Black" Command="{Binding Path=btnAddGuest}" Content="Novi Gost" FontFamily="Times New Roman" FontWeight="Bold" Height="25" HorizontalAlignment="Left" IsDefault="True" Margin="266,10,0,0" Name="btnNewGuest" VerticalAlignment="Top" Width="62" />
BookingsViewModel.cs
private tblGuest guest;
public tblGuest Guest // Selected guest from combo box
{
get
{
return guest;
}
set
{
guest = value;
OnPropertyChanged("Guest");
}
}
private ObservableCollection<tblGuest> guests;
public ObservableCollection<tblGuest> Guests // Guests list in the combo box
{
get
{
return guests;
}
set
{
guests = value;
OnPropertyChanged("Guests");
}
}
public ICommand _btnAddGuest;
public ICommand btnAddGuest // Command for opening child window
{
get
{
if (_btnAddGuest == null)
{
_btnAddGuest = new DelegateCommand(delegate()
{
try
{
Guests guest = new Guests();
guest.ShowDialog();
}
catch
{
Trace.WriteLine("working...", "MyApp");
}
});
}
return _btnAddGuest;
}
}
Guests.xaml
<Button Command="{Binding Path= btnAddGuest}" Content="Dodaj" FontFamily="Times New Roman" FontWeight="Bold" Height="36" HorizontalAlignment="Left" Margin="12,402,0,0" Name="btnAddGuest" VerticalAlignment="Top" Width="62" IsDefault="True" />
This button (in Guest.xaml window) adds a new guest into database.
GuestViewModel.cs
private tblGuest guest;
public tblGuest Guest // Guest to be added into database
{
get
{
return guest;
}
set
{
guest = value;
OnPropertyChanged("Guest");
}
}
public ICommand _btnAddGuest;
public ICommand btnAddGuest // Command for adding new guest
{
get
{
if (_btnAddGuest == null)
{
_btnAddGuest = new DelegateCommand(delegate()
{
try
{
Service1Client wcf = new Service1Client();
wcf.AddGuest(Guest); // "AddGuest()" WCF method adds new guest to database
wcf.Close();
}
catch
{
Trace.WriteLine("working...", "MyApp");
}
});
}
return _btnAddGuest;
}
}
How to solve this problem? Is there any easy way? Can you, please, explain in detail your solution because I'm new in WPF, WCF and MVVM...
Best regards, Vladimir