Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. still shown DialogService until error exists in user input and dont press Cancel

still shown DialogService until error exists in user input and dont press Cancel

Scheduled Pinned Locked Moved WPF
help
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrKBA
    wrote on last edited by
    #1

    Hi, I included DialogService in my Window to call other view which contains User input values What I need is to prevent closing this dialog until input are validated.

    _roleViewModel = new Role_ViewModel(eTypeOperation);
    var createCommand = new UICommand
    {
    Id = MessageBoxResult.OK,
    Caption = Properties.Resources.CstOk,
    IsCancel = false,
    IsDefault = true,
    Command = new DelegateCommand(CreateRole, CanCreateRole)
    };
    var cancelCommand = new UICommand
    {
    Id = MessageBoxResult.Cancel,
    Caption = Properties.Resources.CstCancel,
    IsCancel = true,
    IsDefault = false,
    };
    RoleService.ShowDialog(new List { createCommand, cancelCommand }, Properties.Resources.CstNewRole, _roleViewModel);

    private IDialogService RoleService
    {
    get { return GetService("RoleServiceDialog"); }
    }

    the CanCreateRole control the state of Button "OK" regarding input user But What I need is to : When click OK button , we should check data (user input) if it is ok we return and close the DialogService otherwise we should display messagebox and stay on this DialogService (don't close) until press "Cancel" Thank you

    Richard DeemingR 1 Reply Last reply
    0
    • M MrKBA

      Hi, I included DialogService in my Window to call other view which contains User input values What I need is to prevent closing this dialog until input are validated.

      _roleViewModel = new Role_ViewModel(eTypeOperation);
      var createCommand = new UICommand
      {
      Id = MessageBoxResult.OK,
      Caption = Properties.Resources.CstOk,
      IsCancel = false,
      IsDefault = true,
      Command = new DelegateCommand(CreateRole, CanCreateRole)
      };
      var cancelCommand = new UICommand
      {
      Id = MessageBoxResult.Cancel,
      Caption = Properties.Resources.CstCancel,
      IsCancel = true,
      IsDefault = false,
      };
      RoleService.ShowDialog(new List { createCommand, cancelCommand }, Properties.Resources.CstNewRole, _roleViewModel);

      private IDialogService RoleService
      {
      get { return GetService("RoleServiceDialog"); }
      }

      the CanCreateRole control the state of Button "OK" regarding input user But What I need is to : When click OK button , we should check data (user input) if it is ok we return and close the DialogService otherwise we should display messagebox and stay on this DialogService (don't close) until press "Cancel" Thank you

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      In your CanCreateRole method, validate the input, and return false if the input is not valid. In your CreateRole method, call CanCreateRole to validate the input, and if it's not valid, don't close the dialog.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        In your CanCreateRole method, validate the input, and return false if the input is not valid. In your CreateRole method, call CanCreateRole to validate the input, and if it's not valid, don't close the dialog.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        MrKBA
        wrote on last edited by
        #3

        But this did'nt solve my problem :) because what I need is : - let user enter some data (if no data => the button still disabled related to

        CanCreateRole

        - when there is data click on Button "OK" - if the data are valid against condition we do nothing and the DialogService close (normal behavior) ELSE (data not valid ) we display messagebox and the DialogService is still shown with last data entered.

        Richard DeemingR 1 Reply Last reply
        0
        • M MrKBA

          But this did'nt solve my problem :) because what I need is : - let user enter some data (if no data => the button still disabled related to

          CanCreateRole

          - when there is data click on Button "OK" - if the data are valid against condition we do nothing and the DialogService close (normal behavior) ELSE (data not valid ) we display messagebox and the DialogService is still shown with last data entered.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          So do your extra validation in the CreateRole method, and don't close the dialog if it fails. Or are you saying that the dialog always closes after invoking the command? If that's the case, we'll need to know what the implementation of your dialog service looks like.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          M 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            So do your extra validation in the CreateRole method, and don't close the dialog if it fails. Or are you saying that the dialog always closes after invoking the command? If that's the case, we'll need to know what the implementation of your dialog service looks like.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            M Offline
            M Offline
            MrKBA
            wrote on last edited by
            #5

            the code of command is as described in the first question :

            var createCommand = new UICommand
            {
            Id = MessageBoxResult.OK,
            Caption = Properties.Resources.CstOk,
            IsCancel = false,
            IsDefault = true,
            Command = new DelegateCommand(CreateRole, CanCreateRole)
            };

            Richard DeemingR 1 Reply Last reply
            0
            • M MrKBA

              the code of command is as described in the first question :

              var createCommand = new UICommand
              {
              Id = MessageBoxResult.OK,
              Caption = Properties.Resources.CstOk,
              IsCancel = false,
              IsDefault = true,
              Command = new DelegateCommand(CreateRole, CanCreateRole)
              };

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Yes, but we can't see your dialog service code. If it's something you've written, then you need to show the relevant parts of the code. If it's something you got from an article or GitHub project, then we'll need a link to the source.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              M 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Yes, but we can't see your dialog service code. If it's something you've written, then you need to show the relevant parts of the code. If it's something you got from an article or GitHub project, then we'll need a link to the source.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                M Offline
                M Offline
                MrKBA
                wrote on last edited by
                #7

                In my MainWindow.xaml (which will validate data after click on OK )

                in MainWindow.cs (the ViewModel related to the MainWindow.xaml)

                   \_roleViewModel = new Role\_ViewModel(eTypeOperation);
                        var createCommand = new UICommand
                        {
                            Id = MessageBoxResult.OK,
                            Caption = Properties.Resources.CstOk,
                            IsCancel = false,
                            IsDefault = true,
                            Command = new DelegateCommand(CreateRole, CanCreateRole)
                        };
                
                        var cancelCommand = new UICommand
                        {
                            Id = MessageBoxResult.Cancel,
                            Caption = Properties.Resources.CstCancel,
                            IsCancel = true,
                            IsDefault = false,
                        };
                
                        RoleService.ShowDialog(new List { createCommand, cancelCommand},Properties.Resources.CstNewRole, \_roleViewModel);
                

                private void CreateRole()
                {
                _roleViewModel.AcceptChanges();
                if (ValidateData(_roleViewModel.RoleName.Trim()))
                {
                AddRole(_roleViewModel.RoleName, _roleViewModel.RoleDescription);
                }
                }

                private bool CanCreateRole()
                {
                return !string.IsNullOrEmpty(_roleViewModel.RoleName.Trim());
                }

                Richard DeemingR 1 Reply Last reply
                0
                • M MrKBA

                  In my MainWindow.xaml (which will validate data after click on OK )

                  in MainWindow.cs (the ViewModel related to the MainWindow.xaml)

                     \_roleViewModel = new Role\_ViewModel(eTypeOperation);
                          var createCommand = new UICommand
                          {
                              Id = MessageBoxResult.OK,
                              Caption = Properties.Resources.CstOk,
                              IsCancel = false,
                              IsDefault = true,
                              Command = new DelegateCommand(CreateRole, CanCreateRole)
                          };
                  
                          var cancelCommand = new UICommand
                          {
                              Id = MessageBoxResult.Cancel,
                              Caption = Properties.Resources.CstCancel,
                              IsCancel = true,
                              IsDefault = false,
                          };
                  
                          RoleService.ShowDialog(new List { createCommand, cancelCommand},Properties.Resources.CstNewRole, \_roleViewModel);
                  

                  private void CreateRole()
                  {
                  _roleViewModel.AcceptChanges();
                  if (ValidateData(_roleViewModel.RoleName.Trim()))
                  {
                  AddRole(_roleViewModel.RoleName, _roleViewModel.RoleDescription);
                  }
                  }

                  private bool CanCreateRole()
                  {
                  return !string.IsNullOrEmpty(_roleViewModel.RoleName.Trim());
                  }

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  So you're using the DevExpress MVVM framework? Looking at the documentation[^], your command can take a CancelEventArgs parameter to allow it to keep the dialog open:

                  Online Documentation - Developer Express Inc.[^]:

                  Note that dialog commands take a CancelEventArgs object as a parameter. When dialog commands are invoked, the dialog is closed by default. To prevent this behavior, it is necessary to set the CancelEventArgs.Cancel parameter to True.

                  var createCommand = new UICommand
                  {
                  Id = MessageBoxResult.OK,
                  Caption = Properties.Resources.CstOk,
                  IsCancel = false,
                  IsDefault = true,
                  Command = new DelegateCommand<CancelEventArgs>(CreateRole, CanCreateRole)
                  };

                  private bool CanCreateRole(CancelEventArgs e)
                  {
                  // NB: IsNullOrWhiteSpace(value) is more efficient than IsNullOrEmpty(value.Trim()),
                  // and avoids a NullReferenceException if the string is null.
                  return !string.IsNullOrWhiteSpace(_roleViewModel.RoleName);
                  }

                  private void CreateRole(CancelEventArgs e)
                  {
                  _roleViewModel.AcceptChanges();
                  if (ValidateData(_roleViewModel.RoleName.Trim()))
                  {
                  AddRole(_roleViewModel.RoleName, _roleViewModel.RoleDescription);
                  }
                  else
                  {
                  // Keep the dialog open:
                  e.Cancel = true;
                  }
                  }


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  M 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    So you're using the DevExpress MVVM framework? Looking at the documentation[^], your command can take a CancelEventArgs parameter to allow it to keep the dialog open:

                    Online Documentation - Developer Express Inc.[^]:

                    Note that dialog commands take a CancelEventArgs object as a parameter. When dialog commands are invoked, the dialog is closed by default. To prevent this behavior, it is necessary to set the CancelEventArgs.Cancel parameter to True.

                    var createCommand = new UICommand
                    {
                    Id = MessageBoxResult.OK,
                    Caption = Properties.Resources.CstOk,
                    IsCancel = false,
                    IsDefault = true,
                    Command = new DelegateCommand<CancelEventArgs>(CreateRole, CanCreateRole)
                    };

                    private bool CanCreateRole(CancelEventArgs e)
                    {
                    // NB: IsNullOrWhiteSpace(value) is more efficient than IsNullOrEmpty(value.Trim()),
                    // and avoids a NullReferenceException if the string is null.
                    return !string.IsNullOrWhiteSpace(_roleViewModel.RoleName);
                    }

                    private void CreateRole(CancelEventArgs e)
                    {
                    _roleViewModel.AcceptChanges();
                    if (ValidateData(_roleViewModel.RoleName.Trim()))
                    {
                    AddRole(_roleViewModel.RoleName, _roleViewModel.RoleDescription);
                    }
                    else
                    {
                    // Keep the dialog open:
                    e.Cancel = true;
                    }
                    }


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    M Offline
                    M Offline
                    MrKBA
                    wrote on last edited by
                    #9

                    Great, saved me :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups