2011-04-12 39 views
7

我是新來的MVVM,並試圖找出如何關閉ChildWindow與傳統取消使用MVVM光工具包鍵取消按鈕ChildWindow。如何關閉使用MVVM光工具包

在我ChildWindow(StoreDetail.xaml),我有:

<Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelCommand}" /> 

在我的視圖模型(ViewModelStoreDetail.cs),我有:

public ICommand CancelCommand { get; private set; } 

public ViewModelStoreDetail() 
{ 
    CancelCommand = new RelayCommand(CancelEval); 
} 

private void CancelEval() 
{ 
    //Not sure if Messenger is the way to go here... 
    //Messenger.Default.Send<string>("ClosePostEventChildWindow", "ClosePostEventChildWindow"); 
} 

回答

0

看一看this articleon MSDN。大約一半的時候,有一個關於如何做到這一點的方法。基本上它使用的是使用WorkspaceViewModel或者你實現一個公開的接口和事件RequestClose

然後你在窗口的DataContext裏面(如果你設置ViewModel的話)你可以附加到事件上。

這是文章摘錄(圖7)。您可以調整它以適應您的需求。

// In App.xaml.cs 
protected override void OnStartup(StartupEventArgs e) 
{ 
base.OnStartup(e); 

MainWindow window = new MainWindow(); 

// Create the ViewModel to which 
// the main window binds. 
string path = "Data/customers.xml"; 
var viewModel = new MainWindowViewModel(path); 

// When the ViewModel asks to be closed, 
// close the window. 
viewModel.RequestClose += delegate 
{ 
    window.Close(); 
}; 

// Allow all controls in the window to 
// bind to the ViewModel by setting the 
// DataContext, which propagates down 
// the element tree. 
window.DataContext = viewModel; 

window.Show(); 
} 
0

這已經有一段時間,因爲我用WPF和MVVMLight不過是我想我會使用信使發送取消事件。

0

In MVVM Light Toolkit您可以做的最好的事情就是使用MessengerView進行交互。

只需在View(通常在文件後面的代碼中)註冊關閉方法,然後發送請求以在需要時關閉窗口。

1
private DelegateCommand _cancelCommand; 

public ICommand CancelCommand 
{ 
    get 
    { 
     if (_cancelCommand == null) 
      _cancelCommand = new DelegateCommand(CloseWindow); 
     return _cancelCommand; 
    } 
} 

private void CloseWindow() 
{ 
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close(); 
} 
0

這裏有一些方法來完成它。

  1. 將消息發送到您的子窗口並在子窗口代碼隱藏中將DialogueResult設置爲false。
  2. 設置DialogueResult的屬性並綁定它的子窗口Dialoue CLR屬性,將其設置爲CancelCommand的CancelEval方法。
  3. 創建Childwindow的對象並在CancelEval上設置DialogueResult false。
1

如果您通過調用ShowDialog()來顯示您的子窗口,那麼您可以簡單地將您的按鈕控件的IsCancel屬性設置爲「True」。

<Button Content="Cancel" IsCancel="True" /> 

它與單擊窗口上的X按鈕或按鍵盤上的ESC相同。

0

晚會的種類,但我想我會添加我的輸入。從user841960的回答借鑑:

public RelayCommand CancelCommand 
{ 
    get; 
    private set; 
} 

然後:

SaveSettings = new RelayCommand(() => CloseWindow()); 

然後:

private void CloseWindow() 
{ 
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close(); 
} 

這比使用一個ICommand清潔了一下,效果一樣好。

所以,總括起來,例如類看起來就像這樣:

public class ChildViewModel 
{ 
    public RelayCommand CancelCommand 
    { 
     get; 
     private set; 
    } 

    public ChildViewModel() 
    { 
     SaveSettings = new RelayCommand(() => CloseWindow()); 
    } 

    private void CloseWindow() 
    { 
     Application.Current.Windows[Application.Current.Windows.Count - 1].Close(); 
    } 
} 
+0

將在什麼地方SaveSettings = ...去? – admiral142 2014-07-31 20:26:39

+0

在ViewModel構造函數中。一個完整的例子可以在這裏找到(http://msdn.microsoft.com/en-us/magazine/dn237302.aspx)以及Command,RelayCommand和EventToCommand文檔。 – DanteTheEgregore 2014-07-31 21:56:33