1

我正在开发一些酒店业务应用程序。我有一个“BookingsWindow”,用于将新预订插入数据库。在此窗口中,有一个组合框,其中包含酒店中所有客人的列表。“BookingsWindow”是一个父窗口。它包含一个用于添加新客人的按钮。当您单击该按钮时,将打开新的 winodw(“GuestWindow” - 子窗口)。两个窗口都有自己的视图模型(“BookingsViewModel”和“GuestsViewModel”)。父窗口中组合框的项目源绑定到属性“Guests”,该属性是具有来自数据库表的值的可观察集合。我的问题是,从子窗口添加新来宾后如何更新组合框项目源?

BookingsWindow.xaml

<ComboBox Height="25" HorizontalAlignment="Left" IsEditable="True"   IsTextSearchEnabled="True" ItemsSource="{Binding Path=Guests}" Margin="119,10,0,0" Name="cbGuest" Padding="3,1,1,1" SelectedItem="{Binding Path=Guest}" 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 ObservableCollection<ServiceReference1.tblGuest> guests;
    public ObservableCollection<ServiceReference1.tblGuest> Guests
    {
        get
        {
            return guests;
        }
        set
        {
            guests = value;
            OnPropertyChanged("Guests");               
        }
    }

    public ICommand _btnAddGuest;
    public ICommand btnAddGuest
    {
        get
        {
            if (_btnAddGuest == null)
            {
                _btnAddGuest = new DelegateCommand(delegate()
                {
                    try
                    {
                        GuestsViewModel gst = new GuestsViewModel();

                        Guests guest = new Guests();
                        guest.ShowDialog();
                    }
                    catch
                    {
                        Trace.WriteLine("working...", "MyApp");
                    }
                });
            }
            return _btnAddGuest;
        }

客人.xaml

<Button 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" Command="{Binding Path= btnAddGuest}" />

btnAddGuest是用于将新访客添加到数据库的命令(“tblGuests”)

客人ViewModel

    private ServiceReference1.tblGuest guest;
    public ServiceReference1.tblGuest Guest 
    {
        get 
        {
            return guest;
        }
        set 
        {
            guest = value;
            OnPropertyChanged("Guest");
        }
    }       



    public ICommand _btnAddGuest;
    public ICommand btnAddGuest 
    {
        get
        {
            if (_btnAddGuest == null)
            {
                _btnAddGuest = new DelegateCommand(delegate()
                {
                    try
                    {
                        Service1Client wcf = new Service1Client();                           

                        wcf.AddGuest(Guest);
                        wcf.Close();          
                    }
                    catch
                    {
                        Trace.WriteLine("working...", "MyApp");
                    }
                });
            }
            return _btnAddGuest;
        }
    }

wcf.AddGuest是 WCF 服务上用于将新来宾添加到数据库的方法...

当我将新来宾添加到数据库时,组合框的列表中没有该新来宾。如何管理这个?

4

0 回答 0