2

我正在开发一些应用程序,但我遇到了一个问题。我有两个窗口(预订 - 父母和客人 - 孩子)。在父窗口中,我有一个包含客人列表的组合框和一个用于添加新客人的按钮。当我单击该按钮时,访客窗口(子窗口)将打开。在子窗口中,我将新来宾添加到数据库中,效果很好。我的问题是:在子窗口中添加新来宾后如何刷新/更新父窗口中的组合框列表?我知道属性的更改应该反映在视图中,而无需从数据库中检索数据(感谢绑定)。

预订.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 的新手...

最好的问候,弗拉基米尔

4

6 回答 6

2

在课堂上保留BookingsViewModel实例并在添加新访客时调用(行后)。GuestViewModelBookingsViewModel.OnPropertyChanged("Guest")wcf.AddGuest(Guest);

于 2013-06-25T08:41:17.077 回答
1

只需使用您已经存在的连接到您GuestViewModelBookingsViewModel.

以下建议未经测试,但您会明白的

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;
        }
    }
于 2013-06-25T11:52:04.510 回答
0

如果我正确理解了问题——你想从子窗口中获得一个新的客人并在父窗口中更新你的收藏——那么有很多方法可以做到这一点。

例如:

  1. 将事件“OnSuccessful”添加到您的 GuestViewModel,然后附加 BookingsViewModel。

  2. 实现 EventAggregator 模式(订阅者 - 监听者)

  3. 关闭子窗口后只需刷新客人(将其显示为模式对话框)。

于 2013-06-24T15:34:35.993 回答
0

好的,我在“GuestsViewModel.cs”中添加了这些行:

    private BookingsViewModel _BookingsViewModel;
    public BookingsViewModel BookViewModel    // Property
    {
        get
        {
            return _BookingsViewModel;
        }
        set
        {
            _BookingsViewModel = value;
            OnPropertyChanged("BookViewModel");
        }
    }

    public GuestsViewModel(BookingsViewModel bvm)    // Constructor with one parameter
    {
        BookViewModel = bvm;
    }

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

                        wcf.AddGuest(Guest);
                        BookingsViewModel.OnPropertyChanged("Guests");    // You said to add this
                        wcf.Close();
                    }
                    catch
                    {
                        Trace.WriteLine("working...", "MyApp");
                    }
                });
            }
            return _btnAddGuest;
        }
    }

上面的意思是这样的吗?这不起作用......我想我应该再添加一件事?对不起,也许这个问题很愚蠢,但我不知道如何解决这个问题......

问候, 弗拉基米尔

于 2013-06-25T11:27:01.513 回答
0

一个简单的回调接口可以帮助你。客户端应该订阅,如果服务器推送通知,则填充组合框或刷新。

这是一个教程:http: //idunno.org/archive/2008/05/29/wcf-callbacks-a-beginners-guide.aspx

我曾经创建一个“登录”方法并将每个客户端回调存储在服务器端。然后,每当我需要使用推送通知时,我只需使用这个存储的客户端回调。在客户端,您可以根据需要处理收到的消息/事件。

于 2013-06-25T09:18:18.563 回答
0

如果您从主窗口和子视图模型创建子窗口,您可以查看窗口关闭事件,或者如果您需要在窗口关闭之前知道,您还可以订阅子项的属性更改事件窗口创建时,当子视图模型上的属性更改时,您可以检查它是否是您想要的属性。如果是这样,请更新您的列表。

ChildViewModel n = new ChildViewModel()
n.PropertyChanged += new PropertyChangedEventHandler(n_PropertyChanged);
void n_PropertyChanged(object sender, PropertyChangedEventArgs e)
{ if(e.PropertyName == "myChangingObject")
//reload list
}
于 2013-06-24T15:38:42.853 回答