0

I have to click button, that save changes and close window.

public BaseCommand SaveCommand => saveCommand ?? (saveCommand = new BaseCommand(SaveMetod, SaveCanMetod));
private bool SaveCanMetod() => IsSelectedCamera && (SelectedCamera.Height != CameraHeight || SelectedCamera.Width != CameraWidth);

        private void SaveMetod()
        {
            if (SaveCanMetod())
            {
                SelectedCamera.Width = CameraWidth.Value;
                SelectedCamera.Height = CameraHeight.Value;                
                //Application.Current.MainWindow.Close();

            }
        }

The string "Application.Current.MainWindow.Close();" don't work when I start my application on revit.

Spike
  • 43
  • 7
  • Are you want close whole app or just a window? – AmRo Aug 06 '19 at 15:30
  • @AmRo I am want close just a window – Spike Aug 06 '19 at 15:33
  • You can pass the window as a command parameter and close it after your jobs done. To passing window as a command parameter see this example [Passing the current Window as a CommandParameter](https://stackoverflow.com/a/3504631/11219312). – AmRo Aug 06 '19 at 15:38
  • I don't understand what I can do this, I already pass my SaveCommand – Spike Aug 06 '19 at 16:00

1 Answers1

0

See this example.

in xaml:

<Window Name="ThisWindow">
.
.
.
<Button Command="{Binding SaveCommand}" CommandParameter="{Binding ElementName=ThisWindow}" />

in code behind:

private void SaveMethod(object paramter)
{
    var currentWindow = (Window)paramter;
    // TODO: ...
    currentWindow.Close();
}

and command:

public class RelayCommand : ICommand    
{    
    private Action<object> execute;    
    private Func<object, bool> canExecute;    

    public event EventHandler CanExecuteChanged    
    {    
        add { CommandManager.RequerySuggested += value; }    
        remove { CommandManager.RequerySuggested -= value; }    
    }    

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)    
    {    
        this.execute = execute;    
        this.canExecute = canExecute;    
    }    

    public bool CanExecute(object parameter)    
    {    
        return this.canExecute == null || this.canExecute(parameter);    
    }    

    public void Execute(object parameter)    
    {    
        this.execute(parameter);    
    }    
}  
AmRo
  • 833
  • 1
  • 8
  • 19
  • I write my code with using MVVM pattern, and have many classes – Spike Aug 07 '19 at 08:57
  • I think you pass the User Control as parameter instead of Window. Place your window Xaml codes that's contains this button. – AmRo Aug 07 '19 at 12:10