我正在处理某些应用程序,但有一个问题。
我有两个窗口(预订-父级和 guest -子级)。在父窗口中,我有一个包含 guest 列表的组合框和一个用于添加新 guest 的按钮。当我单击该按钮时,“ guest ”窗口(子窗口)打开。在子窗口中,我正在向数据库中添加新的 guest ,并且工作正常。
我的问题是:在子窗口中添加新 guest 后,如何在父窗口中刷新/更新组合框列表?我知道应该在 View 中反射(reflect)该属性的更改,而无需从数据库中检索数据(由于绑定(bind))。
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" />
此按钮(在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的新手,请您能详细解释您的解决方案...
最好的祝福,
弗拉基米尔
最佳答案
只需使用GuestViewModel
中与BookingsViewModel
的现有连接即可。
以下建议未经测试,但您会明白的
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;
}
}
关于c# - 从其他窗口中刷新组合框列表,MVVM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17279110/