11

我正在尝试使用 Silverlight (3) 中的 MVVM 模式从 ChildWindow/popup 中获取数据的正确方法。例如:我有一个带有数据输入表单的主页,我想打开一个包含客户列表的弹出窗口。当用户选择客户时,我想将所选客户转移到主页。这就是我目前正在使用的(示例)代码:

主页

public partial class MainPage : UserControl
{
    public MainPageViewModel ViewModel { get; private set; }

    public MainPage()
    {
        InitializeComponent();
        ViewModel = new MainPageViewModel();
        DataContext = ViewModel;
    }

    private void SearchCustomer_Click(object sender, RoutedEventArgs e)
    {
        ViewModel.SearchCustomer();
    }
}

public class MainPageViewModel: ViewModel
{
    private string customer;
    public string Customer
    {
        get { return customer; }
        set { customer = value; RaisePropertyChanged("Customer"); }
    }

    public void SearchCustomer()
    {
        // Called from a view
        SearchWindow searchWindow = new SearchWindow();
        searchWindow.Closed += (sender, e) =>
        {
            if ((bool)searchWindow.DialogResult)
            {
                Customer = searchWindow.ViewModel.SelectedCustomer.ToString();
            }
        };
        searchWindow.Show();
    }
}

子窗口

public partial class SearchWindow : ChildWindow
{
    public SearchWindowViewModel ViewModel { get; private set; }

    public SearchWindow()
    {
        InitializeComponent();
        ViewModel = new SearchWindowViewModel();
        DataContext = ViewModel;
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = ViewModel.OkButtonClick();
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = ViewModel.CancelButtonClick();
    }
}

public class SearchWindowViewModel: ViewModel
{
    private Customer selectedCustomer;        
    private ObservableCollection<Customer> customers;

    public ObservableCollection<Customer> Customers
    {
        get { return customers; }
        set {customers = value; RaisePropertyChanged("Customers"); }
    }

    public Customer SelectedCustomer
    {
        get { return selectedCustomer; }
        set { selectedCustomer = value; RaisePropertyChanged("SelectedCustomer"); }
    }

    public SearchWindowViewModel()
    {
        Customers = new ObservableCollection<Customer>();
        ISearchService searchService = new FakeSearchService();
        foreach (Customer customer in searchService.FindCustomers("dummy"))
            Customers.Add(customer);
    }

    public bool? OkButtonClick()
    {
        if (SelectedCustomer != null)
            return true;
        else
            return null; // show some error message before that
    }

    public bool? CancelButtonClick()
    {
        return false;
    }
}

这是正确的方法还是有更“简单”的方法?

干杯,韩国

4

2 回答 2

6

这里更成问题的是在 VM 中使用 View 特定的术语和类型。点击事件,DialogResults 不应该在您的 ViewModels 附近。

关于这个问题,我在这里有一个类似的问题: Handling Dialogs in WPF with MVVM

我接受的答案是使用中介者模式来解决这个问题。看一看。:)

于 2009-04-20T11:53:00.423 回答
1

Chinch mvvm helper library是一个很好的支持打开子窗口的 MVVM库。您可以在http://www.codeproject.com/KB/silverlight/SL4FileUploadAnd_SL4_MVVM.aspx查看示例。

于 2010-07-02T09:03:13.163 回答