我正在开发一些应用程序,但我遇到了一个问题。我有两个窗口(预订 - 父母和客人 - 孩子)。在父窗口中,我有一个包含客人列表的组合框和一个用于添加新客人的按钮。当我单击该按钮时,访客窗口(子窗口)将打开。在子窗口中,我将新来宾添加到数据库中,效果很好。我的问题是:在子窗口中添加新来宾后如何刷新/更新父窗口中的组合框列表?我知道属性的更改应该反映在视图中,而无需从数据库中检索数据(感谢绑定)。
预订.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;
}
}
客人.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" />
此按钮(在 Guest.xaml 窗口中)将新来宾添加到数据库中。
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;
}
}
如何解决这个问题呢?有什么简单的方法吗?请您详细解释您的解决方案,因为我是 WPF、WCF 和 MVVM 的新手...
最好的问候,弗拉基米尔