Search code examples
c#wpfmvvmrevit

Close window wpf in Revit


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.


Solution

  • 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);    
        }    
    }