2013-03-04 44 views
18

có cách nào để xử lý nút đóng cửa sổ tức là "X" ở góc trên cùng bên phải trong chế độ xem bằng cách liên kết với lệnh không? hoặc ghi đè lệnh window.close để đóng một cửa sổ quay lại cửa sổ trước đó. Thanx.nút đóng cửa sổ xử lý trong wpf MVVM

Trả lời

30

Có một số phương pháp cho việc này. Tôi đã chỉ ra hai phương pháp dưới đây.

  1. Bạn có thể sử dụng các lệnh đính kèm để ràng buộc nút đóng trong mô hình chế độ xem của mình.

  2. Bạn có thể sử dụng Dưới đây đang

XAML:

<Window x:Class="WpfInfragisticsModal.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" 
     xmlns:ig="http://schemas.infragistics.com/xaml" 
     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
     Name="myWindow"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Closing"> 
      <i:InvokeCommandAction Command="{Binding CloseWindowCommand}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <Grid> 
    </Grid> 
</Window> 

LƯU Ý: Thêm System.Windows.Interactivity tham khảo

Xem Mẫu

private ICommand closeWindowCommand; 

public ICommand CloseWindowCommand 
{ 
     get 
     { 
      if (closeWindowCommand == null) 
      { 
      closeWindowCommand = new RelayCommand(param => this.CloseWindow(), null); 
      } 
      return closeWindowCommand; 
     } 
} 

private void CloseWindow() 
{ 
    //Do your operations 
} 

Đây là lớp RelayCommand của tôi.

public class RelayCommand : ICommand 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="RelayCommand"/> class. 
    /// </summary> 
    /// <param name="execute">The execute.</param> 
    public RelayCommand(Action<object> execute) 
     : this(execute, null) 
    { 
    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="RelayCommand"/> class. 
    /// </summary> 
    /// <param name="execute">The execute.</param> 
    /// <param name="canExecute">The can execute.</param> 
    public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
    { 
     if (execute == null) 
      throw new ArgumentNullException("execute"); 
     _execute = execute; 
     _canExecute = canExecute; 
    } 

    /// <summary> 
    /// Defines the method that determines whether the command can execute in its current state. 
    /// </summary> 
    /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> 
    /// <returns> 
    /// true if this command can be executed; otherwise, false. 
    /// </returns> 
    public bool CanExecute(object parameter) 
    { 
     return _canExecute == null ? true : _canExecute(parameter); 
    } 

    /// <summary> 
    /// Occurs when changes occur that affect whether or not the command should execute. 
    /// </summary> 
    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    /// <summary> 
    /// Defines the method to be called when the command is invoked. 
    /// </summary> 
    /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> 
    public void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

    /// <summary> 
    /// Action 
    /// </summary> 
    private readonly Action<object> _execute; 


    /// <summary> 
    /// Predicate 
    /// </summary> 
    private readonly Predicate<object> _canExecute; 
} 
+0

Chính xác như thế này, MVVM Light có thể giúp bạn! –

+0

thanx haritha, tôi đã thử nó nhưng nó đã không làm việc cho tôi, tôi couldnt tìm 'system.windows.interactivity' không gian tên .. tôi đang sử dụng VS2010..some đề nghị 'windowsbase' nhưng thậm chí không làm việc để có được xmlns: i = "http://schemas.microsoft.com/expression/2010/interactivity" để làm việc ... –

+1

Hi Andris, Bạn phải cài đặt một redistributable để xuất hiện trong Visual Studio. Bạn có thể tải xuống tại đây (http://www.microsoft.com/en-us/download/details.aspx?id=10801). – Haritha

0

10 vấn đề là tôi đã đóng cửa sổ cha và mở lại sau khi đóng cửa sổ con tương ứng, gây rò rỉ bộ nhớ. Tôi giải quyết bằng cách ẩn cửa sổ cha và sau đó hiển thị lại sau khi cửa sổ con đóng. Tôi mới để phát triển wpf và cửa sổ vì vậy tôi học như tôi đi.

Các vấn đề liên quan