这与此处描述的问题相同,从另一个窗口刷新组合框列表,但我有一个 ListView 而不是 ComboBox。我使用了与上面链接中描述的相同的方法,但它不能正常工作。我的 ListView 有 6 列,它绑定到 MVVM 属性(“Bookings”)。现在,这些列从 3 个不同的数据库表中获取数据。添加新的预订后,只有从表“tblBookings”中获取数据的列填充了值,并且没有从该表中获取数据的列是空的,直到我重新启动我的窗口。这是代码:
预订视图模型:
private bool isBookingSaved;
public bool IsBookingSaved
{
get
{
return isBookingSaved;
}
set
{
isBookingSaved = value;
OnPropertyChanged("IsBookingSaved");
}
}
private string type;
public string Type
{
get
{
return type;
}
set
{
type = value;
OnPropertyChanged("Type");
}
}
private ServiceReference1.tblBooking booking;
public ServiceReference1.tblBooking Booking
{
get
{
return booking;
}
set
{
booking = value;
OnPropertyChanged("Booking");
}
}
private ServiceReference1.tblGuest guest;
public ServiceReference1.tblGuest Guest
{
get
{
return guest;
}
set
{
guest = value;
OnPropertyChanged("Guest");
}
}
private tblRoom room;
public tblRoom Room
{
get
{
return room;
}
set
{
room = value;
OnPropertyChanged("Room");
}
}
public ICommand _btnAddBooking;
public ICommand btnAddBooking
{
get
{
if (_btnAddBooking == null)
{
_btnAddBooking = new DelegateCommand(delegate()
{
try
{
Service1Client wcf = new Service1Client();
wcf.AddBooking(Booking, Guest, Room, Type);
IsBookingSaved = true;
wcf.Close();
}
catch
{
Trace.WriteLine("working...", "MyApp");
}
});
}
return _btnAddBooking;
}
}
主窗口.xaml
<ListView SelectionMode="Single" Height="428" HorizontalAlignment="Left" Margin="6,6,0,0" Name="lvReservations" VerticalAlignment="Top" Width="654" BorderBrush="Black" SelectedItem= "{Binding Booking, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=Bookings}" Foreground="Black" >
<ListView.View>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding Path = tblGuest.Name}" Header="Ime i prezime" Width="240" />
<GridViewColumn DisplayMemberBinding="{Binding Path = tblRoom.RoomNo}" Header="Soba" Width="80"/>
<GridViewColumn DisplayMemberBinding="{Binding StartDate, StringFormat=\{0:dd.MM.yyyy.\}}" Header="Od" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding EndDate, StringFormat=\{0:dd.MM.yyyy.\}}" Header="Do" Width="100"/>
<GridViewColumn Header="Aktivno">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsActive}" Name="IsActive" CommandParameter="{Binding}" Command="{Binding DataContext.Activated, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" Click="CheckBox_Click" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Otkazano">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsCanceled}" Name="IsCanceled" CommandParameter="{Binding}" Command="{Binding DataContext.CanceledReservation, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Click="CheckBox_Click" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
主窗口视图模型
private ObservableCollection<tblBooking> bookings;
public ObservableCollection<tblBooking> Bookings
{
get
{
return bookings;
}
set
{
bookings = value;
OnPropertyChanged("Bookings");
}
}
private tblBooking booking;
public tblBooking Booking
{
get
{
return booking;
}
set
{
booking = value;
OnPropertyChanged("Booking");
}
}
public ICommand _btnAddReservation;
public ICommand btnAddReservation
{
get
{
if (_btnAddReservation == null)
{
_btnAddReservation = new DelegateCommand(delegate()
{
try
{
BookingsWindow bookings = new BookingsWindow();
bookings.ShowDialog();
var vm = bookings.DataContext as BookingsViewModel;
if (vm != null)
{
if (vm.IsBookingSaved)
{
var booking = vm.Booking as tblBooking;
Booking = booking;
Bookings.Add(booking);
}
}
}
catch
{
Trace.WriteLine("working...", "MyApp");
}
});
}
return _btnAddReservation;
}
}
有关问题的更多描述,请发表评论,我会更具体。问候。
编辑:
Wcf 服务:
bool IService1.AddBooking(tblBooking booking, tblGuest guest, tblRoom room, string type)
{
try
{
context = new HotelBaseEntities();
tblBooking book = new tblBooking();
tblStayGuest stayGuest = new tblStayGuest();
tblRoom rm = (from r in context.tblRooms where r.RoomID == room.RoomID select r).FirstOrDefault();
book.GuestID = guest.GuestID;
book.RoomID = room.RoomID;
rm.RoomStatus = true;
book.StartDate = booking.StartDate;
book.EndDate = booking.EndDate;
book.PansionType = type;
stayGuest.IsMainGuest = true;
book.IsActive = false;
book.IsCanceled = false;
guest.IsCheckedOut = false;
context.AddTotblBookings(book);
context.SaveChanges();
return true;
}
tblStayGuests 是定义当前在酒店的客人的表。这对我的问题并不重要。正如您在我的绑定中看到的那样,我将 ListView 的项目源绑定到“Bookings”属性,并且从其他列获取数据的列绑定为:例如“Bookings.tblGuest.Name”。在我重新打开窗口之前,像这样绑定的列不会刷新。这就是我要解决的问题。