如何从另一个窗口更新组合框项目源?

时间:2013-06-24 15:21:40

标签: c# wpf wcf mvvm parent-child

我正在开发一些酒店业务应用程序。我有一个“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;
        }

Guests.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”)

GuestsViewModel

    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服务上用于向网络库添加新访客的方法...

当我将新的guest虚拟机添加到数据库时,组合框在其列表中没有该新guest虚拟机。如何管理?

0 个答案:

没有答案