本文介绍了从其他窗口,MVVM刷新框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一些应用程序,我有一个问题。
我有两个窗口(预订 - 家长和客人 - 孩子)。在父窗口中,我有嘉宾列表中的一个组合框,并添加新的客户一个按钮。当我点击该按钮客人窗口(子窗口)打开。在子窗口我加入新的客人到数据库中,并且工作正常。
我的问题是:如何在子窗口中添加新客人后刷新父窗口/更新组合框列表?我知道,在属性更改应在视图中反映出来,而不从数据库中检索数据(感谢绑定)。

I'm working on some application and I have one problem.I have two windows (Bookings - parent and Guests - child). In the parent window, I have one combo box with list of guests and one button for adding new guest. When I click on that button Guests window (child window) opens. In the child window I am adding new guest into database and that works fine.My question is: How to refresh/update combo box list in parent window after adding a new guest in the child window? I know that changes in the property should be reflected in the view without retrieving data from database (thanks to binding).

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窗口)增加了一个新客人到数据库中。

This button (in Guest.xaml window) adds a new guest into database.

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 ...

How to solve this problem? Is there any easy way? Can you, please, explain in detail your solution because I'm new in WPF, WCF and MVVM...

最好的问候,
弗拉基米尔

Best regards,Vladimir

推荐答案

只需使用你对你的 GuestViewModel 已经存在的连接从 BookingsViewModel

Just use your already existent connection to your GuestViewModel from your BookingsViewModel.

以下建议没有进行测试,但你会得到的想法。

The following suggestion is not tested but you will get the idea

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;
        }
    }

这篇关于从其他窗口,MVVM刷新框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 08:25