MVVM模式中的Silverlight子窗口

时间:2009-04-17 07:25:00

标签: silverlight silverlight-3.0 mvvm

我正在尝试使用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;
    }
}

这是正确的方式还是有更“简单”的东西?

干杯, 韩国

2 个答案:

答案 0 :(得分:6)

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

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

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

答案 1 :(得分:1)

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