How to allow pressing only the cancel button when the value is invalid?










0















I have a simple dialog with a SpinEdit and two buttons: OK_Button and Cancel_Button. I've set a mask for the value in the SpinEdit and the dialog won't let me press the cancel button when the value is invalid. I've tried changing the SpinEdit's property to InvalidValueBehavior="AllowLeaveEditor" but then I can click both, OK and cancel button. Is there a way to ONLY allow pressing cancel when the value is incorrect?



XAML:



 <dxe:SpinEdit x:Name="dxSpinEdit" 
Height="23" MinWidth="200" Width="Auto"
HorizontalAlignment="Right"
Text="Binding Value, Mode=TwoWay"
MaskType="Numeric"
IsFloatValue="Binding FloatValue"
MinValue="Binding MinValue"
MaxValue="Binding MaxValue"
Mask="Binding Mask, Mode=OneWay"
MaxLength="Binding Path=InputLength"
MaskShowPlaceHolders="Binding ShowPlaceHolder"
InvalidValueBehavior="WaitForValidValue"
/>

<StackPanel Grid.Row="1" x:Uid="OKCancel_Buttons" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<Button Height="23" x:Name="OK_Button" Click="OK_Click" Content="OK" IsDefault="True" HorizontalAlignment="Right" MinWidth="95" />
<Button Height="23" x:Name="Cancel_Button" Click="Cancel_Click" Content="Cancel" HorizontalAlignment="Right" MinWidth="95" PreviewMouseDown="win_PreviewMouseDown" />
</StackPanel>


I looked up this issue on the devexpress forum but their solution didn't work for me. I've implemented the MouseDownPreview event like so:



C# (code behind)



 private void OK_Click(object sender, RoutedEventArgs e)

DialogResult = true;
Close();


private void Cancel_Click(object sender, RoutedEventArgs e)

DialogResult = false;
Close();


private void win_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)

if(e.Source == Cancel_Button)

DialogResult = false;
Close();




But the event wasn't handled at all. I'd like to keep the property InvalidValueBehavior at the value "WaitForValidValue" but at the same time I'd like to allow pressing the Cancel button.










share|improve this question




























    0















    I have a simple dialog with a SpinEdit and two buttons: OK_Button and Cancel_Button. I've set a mask for the value in the SpinEdit and the dialog won't let me press the cancel button when the value is invalid. I've tried changing the SpinEdit's property to InvalidValueBehavior="AllowLeaveEditor" but then I can click both, OK and cancel button. Is there a way to ONLY allow pressing cancel when the value is incorrect?



    XAML:



     <dxe:SpinEdit x:Name="dxSpinEdit" 
    Height="23" MinWidth="200" Width="Auto"
    HorizontalAlignment="Right"
    Text="Binding Value, Mode=TwoWay"
    MaskType="Numeric"
    IsFloatValue="Binding FloatValue"
    MinValue="Binding MinValue"
    MaxValue="Binding MaxValue"
    Mask="Binding Mask, Mode=OneWay"
    MaxLength="Binding Path=InputLength"
    MaskShowPlaceHolders="Binding ShowPlaceHolder"
    InvalidValueBehavior="WaitForValidValue"
    />

    <StackPanel Grid.Row="1" x:Uid="OKCancel_Buttons" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
    <Button Height="23" x:Name="OK_Button" Click="OK_Click" Content="OK" IsDefault="True" HorizontalAlignment="Right" MinWidth="95" />
    <Button Height="23" x:Name="Cancel_Button" Click="Cancel_Click" Content="Cancel" HorizontalAlignment="Right" MinWidth="95" PreviewMouseDown="win_PreviewMouseDown" />
    </StackPanel>


    I looked up this issue on the devexpress forum but their solution didn't work for me. I've implemented the MouseDownPreview event like so:



    C# (code behind)



     private void OK_Click(object sender, RoutedEventArgs e)

    DialogResult = true;
    Close();


    private void Cancel_Click(object sender, RoutedEventArgs e)

    DialogResult = false;
    Close();


    private void win_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)

    if(e.Source == Cancel_Button)

    DialogResult = false;
    Close();




    But the event wasn't handled at all. I'd like to keep the property InvalidValueBehavior at the value "WaitForValidValue" but at the same time I'd like to allow pressing the Cancel button.










    share|improve this question


























      0












      0








      0








      I have a simple dialog with a SpinEdit and two buttons: OK_Button and Cancel_Button. I've set a mask for the value in the SpinEdit and the dialog won't let me press the cancel button when the value is invalid. I've tried changing the SpinEdit's property to InvalidValueBehavior="AllowLeaveEditor" but then I can click both, OK and cancel button. Is there a way to ONLY allow pressing cancel when the value is incorrect?



      XAML:



       <dxe:SpinEdit x:Name="dxSpinEdit" 
      Height="23" MinWidth="200" Width="Auto"
      HorizontalAlignment="Right"
      Text="Binding Value, Mode=TwoWay"
      MaskType="Numeric"
      IsFloatValue="Binding FloatValue"
      MinValue="Binding MinValue"
      MaxValue="Binding MaxValue"
      Mask="Binding Mask, Mode=OneWay"
      MaxLength="Binding Path=InputLength"
      MaskShowPlaceHolders="Binding ShowPlaceHolder"
      InvalidValueBehavior="WaitForValidValue"
      />

      <StackPanel Grid.Row="1" x:Uid="OKCancel_Buttons" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
      <Button Height="23" x:Name="OK_Button" Click="OK_Click" Content="OK" IsDefault="True" HorizontalAlignment="Right" MinWidth="95" />
      <Button Height="23" x:Name="Cancel_Button" Click="Cancel_Click" Content="Cancel" HorizontalAlignment="Right" MinWidth="95" PreviewMouseDown="win_PreviewMouseDown" />
      </StackPanel>


      I looked up this issue on the devexpress forum but their solution didn't work for me. I've implemented the MouseDownPreview event like so:



      C# (code behind)



       private void OK_Click(object sender, RoutedEventArgs e)

      DialogResult = true;
      Close();


      private void Cancel_Click(object sender, RoutedEventArgs e)

      DialogResult = false;
      Close();


      private void win_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)

      if(e.Source == Cancel_Button)

      DialogResult = false;
      Close();




      But the event wasn't handled at all. I'd like to keep the property InvalidValueBehavior at the value "WaitForValidValue" but at the same time I'd like to allow pressing the Cancel button.










      share|improve this question
















      I have a simple dialog with a SpinEdit and two buttons: OK_Button and Cancel_Button. I've set a mask for the value in the SpinEdit and the dialog won't let me press the cancel button when the value is invalid. I've tried changing the SpinEdit's property to InvalidValueBehavior="AllowLeaveEditor" but then I can click both, OK and cancel button. Is there a way to ONLY allow pressing cancel when the value is incorrect?



      XAML:



       <dxe:SpinEdit x:Name="dxSpinEdit" 
      Height="23" MinWidth="200" Width="Auto"
      HorizontalAlignment="Right"
      Text="Binding Value, Mode=TwoWay"
      MaskType="Numeric"
      IsFloatValue="Binding FloatValue"
      MinValue="Binding MinValue"
      MaxValue="Binding MaxValue"
      Mask="Binding Mask, Mode=OneWay"
      MaxLength="Binding Path=InputLength"
      MaskShowPlaceHolders="Binding ShowPlaceHolder"
      InvalidValueBehavior="WaitForValidValue"
      />

      <StackPanel Grid.Row="1" x:Uid="OKCancel_Buttons" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
      <Button Height="23" x:Name="OK_Button" Click="OK_Click" Content="OK" IsDefault="True" HorizontalAlignment="Right" MinWidth="95" />
      <Button Height="23" x:Name="Cancel_Button" Click="Cancel_Click" Content="Cancel" HorizontalAlignment="Right" MinWidth="95" PreviewMouseDown="win_PreviewMouseDown" />
      </StackPanel>


      I looked up this issue on the devexpress forum but their solution didn't work for me. I've implemented the MouseDownPreview event like so:



      C# (code behind)



       private void OK_Click(object sender, RoutedEventArgs e)

      DialogResult = true;
      Close();


      private void Cancel_Click(object sender, RoutedEventArgs e)

      DialogResult = false;
      Close();


      private void win_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)

      if(e.Source == Cancel_Button)

      DialogResult = false;
      Close();




      But the event wasn't handled at all. I'd like to keep the property InvalidValueBehavior at the value "WaitForValidValue" but at the same time I'd like to allow pressing the Cancel button.







      c# wpf devexpress






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 8:37







      bananeeek

















      asked Nov 16 '18 at 8:09









      bananeeekbananeeek

      2317




      2317






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Even if you're not going to go the full MVVM route, you should switch from using click events to an ICommand implementation that supports CanExecute logic (such as this one from MVVM Light).



          Using a command will automatically disable any bound control (e.g. button or menu item) when CanExecute is false. You can then have all the logic for controlling your commands grouped in one place, including validation that will only allow OK to be clicked when your object is in a valid state.



          If you just want to go the standard WPF (non MVVM) route, you could add something like this in your window's constructor



          public MyView()

          ....


          Ok_Button.Command =
          new RelayCommand(() => DialogResult = true, // just setting DialogResult is sufficient, no need to call Close()
          // put the required validation logic here
          () => dxSpinEdit.Value > 0 && dxSpinEdit.Value < 10);

          Cancel_Button.Command = new RelayCommand(() => DialogResult = false);

          // replace this with the actual event from SpinEdit
          dxSpinEdit.ValueChanged += (s,e) => (OK_Button.Command as RelayCommand).RaiseCanExecuteChanged();



          Yes I know it looks ugly 😀 - I'd suggest following the MVVM design pattern instead. When using MVVM, all of the command functionality belongs in your ViewModel.



          Either way, you can then remove all the click and mousedown handlers from your buttons.






          share|improve this answer

























          • How can I tell those cammands and CanExecute method, that the value inside my SpinEdit is incorrect? I haven't been able to find any solution to that problem. I find it hard to use the example you provided and the others I've found in my situation.

            – bananeeek
            Nov 16 '18 at 9:20











          • @bananeeek - The CanExecute function gets called by WPF when anything changes, and should be able to check the value of your controls.

            – Robin Bennett
            Nov 16 '18 at 9:38











          • Personally I recommend that you don't use the old style CommandManager as you have no control over when CanExecute is called (perhaps after every key stroke or mouse click). Instead you should use an implementation of ICommand that requires you to explicitly call CanExecuteChanged as required.

            – Peregrine
            Nov 16 '18 at 9:45











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333788%2fhow-to-allow-pressing-only-the-cancel-button-when-the-value-is-invalid%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Even if you're not going to go the full MVVM route, you should switch from using click events to an ICommand implementation that supports CanExecute logic (such as this one from MVVM Light).



          Using a command will automatically disable any bound control (e.g. button or menu item) when CanExecute is false. You can then have all the logic for controlling your commands grouped in one place, including validation that will only allow OK to be clicked when your object is in a valid state.



          If you just want to go the standard WPF (non MVVM) route, you could add something like this in your window's constructor



          public MyView()

          ....


          Ok_Button.Command =
          new RelayCommand(() => DialogResult = true, // just setting DialogResult is sufficient, no need to call Close()
          // put the required validation logic here
          () => dxSpinEdit.Value > 0 && dxSpinEdit.Value < 10);

          Cancel_Button.Command = new RelayCommand(() => DialogResult = false);

          // replace this with the actual event from SpinEdit
          dxSpinEdit.ValueChanged += (s,e) => (OK_Button.Command as RelayCommand).RaiseCanExecuteChanged();



          Yes I know it looks ugly 😀 - I'd suggest following the MVVM design pattern instead. When using MVVM, all of the command functionality belongs in your ViewModel.



          Either way, you can then remove all the click and mousedown handlers from your buttons.






          share|improve this answer

























          • How can I tell those cammands and CanExecute method, that the value inside my SpinEdit is incorrect? I haven't been able to find any solution to that problem. I find it hard to use the example you provided and the others I've found in my situation.

            – bananeeek
            Nov 16 '18 at 9:20











          • @bananeeek - The CanExecute function gets called by WPF when anything changes, and should be able to check the value of your controls.

            – Robin Bennett
            Nov 16 '18 at 9:38











          • Personally I recommend that you don't use the old style CommandManager as you have no control over when CanExecute is called (perhaps after every key stroke or mouse click). Instead you should use an implementation of ICommand that requires you to explicitly call CanExecuteChanged as required.

            – Peregrine
            Nov 16 '18 at 9:45















          1














          Even if you're not going to go the full MVVM route, you should switch from using click events to an ICommand implementation that supports CanExecute logic (such as this one from MVVM Light).



          Using a command will automatically disable any bound control (e.g. button or menu item) when CanExecute is false. You can then have all the logic for controlling your commands grouped in one place, including validation that will only allow OK to be clicked when your object is in a valid state.



          If you just want to go the standard WPF (non MVVM) route, you could add something like this in your window's constructor



          public MyView()

          ....


          Ok_Button.Command =
          new RelayCommand(() => DialogResult = true, // just setting DialogResult is sufficient, no need to call Close()
          // put the required validation logic here
          () => dxSpinEdit.Value > 0 && dxSpinEdit.Value < 10);

          Cancel_Button.Command = new RelayCommand(() => DialogResult = false);

          // replace this with the actual event from SpinEdit
          dxSpinEdit.ValueChanged += (s,e) => (OK_Button.Command as RelayCommand).RaiseCanExecuteChanged();



          Yes I know it looks ugly 😀 - I'd suggest following the MVVM design pattern instead. When using MVVM, all of the command functionality belongs in your ViewModel.



          Either way, you can then remove all the click and mousedown handlers from your buttons.






          share|improve this answer

























          • How can I tell those cammands and CanExecute method, that the value inside my SpinEdit is incorrect? I haven't been able to find any solution to that problem. I find it hard to use the example you provided and the others I've found in my situation.

            – bananeeek
            Nov 16 '18 at 9:20











          • @bananeeek - The CanExecute function gets called by WPF when anything changes, and should be able to check the value of your controls.

            – Robin Bennett
            Nov 16 '18 at 9:38











          • Personally I recommend that you don't use the old style CommandManager as you have no control over when CanExecute is called (perhaps after every key stroke or mouse click). Instead you should use an implementation of ICommand that requires you to explicitly call CanExecuteChanged as required.

            – Peregrine
            Nov 16 '18 at 9:45













          1












          1








          1







          Even if you're not going to go the full MVVM route, you should switch from using click events to an ICommand implementation that supports CanExecute logic (such as this one from MVVM Light).



          Using a command will automatically disable any bound control (e.g. button or menu item) when CanExecute is false. You can then have all the logic for controlling your commands grouped in one place, including validation that will only allow OK to be clicked when your object is in a valid state.



          If you just want to go the standard WPF (non MVVM) route, you could add something like this in your window's constructor



          public MyView()

          ....


          Ok_Button.Command =
          new RelayCommand(() => DialogResult = true, // just setting DialogResult is sufficient, no need to call Close()
          // put the required validation logic here
          () => dxSpinEdit.Value > 0 && dxSpinEdit.Value < 10);

          Cancel_Button.Command = new RelayCommand(() => DialogResult = false);

          // replace this with the actual event from SpinEdit
          dxSpinEdit.ValueChanged += (s,e) => (OK_Button.Command as RelayCommand).RaiseCanExecuteChanged();



          Yes I know it looks ugly 😀 - I'd suggest following the MVVM design pattern instead. When using MVVM, all of the command functionality belongs in your ViewModel.



          Either way, you can then remove all the click and mousedown handlers from your buttons.






          share|improve this answer















          Even if you're not going to go the full MVVM route, you should switch from using click events to an ICommand implementation that supports CanExecute logic (such as this one from MVVM Light).



          Using a command will automatically disable any bound control (e.g. button or menu item) when CanExecute is false. You can then have all the logic for controlling your commands grouped in one place, including validation that will only allow OK to be clicked when your object is in a valid state.



          If you just want to go the standard WPF (non MVVM) route, you could add something like this in your window's constructor



          public MyView()

          ....


          Ok_Button.Command =
          new RelayCommand(() => DialogResult = true, // just setting DialogResult is sufficient, no need to call Close()
          // put the required validation logic here
          () => dxSpinEdit.Value > 0 && dxSpinEdit.Value < 10);

          Cancel_Button.Command = new RelayCommand(() => DialogResult = false);

          // replace this with the actual event from SpinEdit
          dxSpinEdit.ValueChanged += (s,e) => (OK_Button.Command as RelayCommand).RaiseCanExecuteChanged();



          Yes I know it looks ugly 😀 - I'd suggest following the MVVM design pattern instead. When using MVVM, all of the command functionality belongs in your ViewModel.



          Either way, you can then remove all the click and mousedown handlers from your buttons.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 9:52

























          answered Nov 16 '18 at 8:51









          PeregrinePeregrine

          2,41021230




          2,41021230












          • How can I tell those cammands and CanExecute method, that the value inside my SpinEdit is incorrect? I haven't been able to find any solution to that problem. I find it hard to use the example you provided and the others I've found in my situation.

            – bananeeek
            Nov 16 '18 at 9:20











          • @bananeeek - The CanExecute function gets called by WPF when anything changes, and should be able to check the value of your controls.

            – Robin Bennett
            Nov 16 '18 at 9:38











          • Personally I recommend that you don't use the old style CommandManager as you have no control over when CanExecute is called (perhaps after every key stroke or mouse click). Instead you should use an implementation of ICommand that requires you to explicitly call CanExecuteChanged as required.

            – Peregrine
            Nov 16 '18 at 9:45

















          • How can I tell those cammands and CanExecute method, that the value inside my SpinEdit is incorrect? I haven't been able to find any solution to that problem. I find it hard to use the example you provided and the others I've found in my situation.

            – bananeeek
            Nov 16 '18 at 9:20











          • @bananeeek - The CanExecute function gets called by WPF when anything changes, and should be able to check the value of your controls.

            – Robin Bennett
            Nov 16 '18 at 9:38











          • Personally I recommend that you don't use the old style CommandManager as you have no control over when CanExecute is called (perhaps after every key stroke or mouse click). Instead you should use an implementation of ICommand that requires you to explicitly call CanExecuteChanged as required.

            – Peregrine
            Nov 16 '18 at 9:45
















          How can I tell those cammands and CanExecute method, that the value inside my SpinEdit is incorrect? I haven't been able to find any solution to that problem. I find it hard to use the example you provided and the others I've found in my situation.

          – bananeeek
          Nov 16 '18 at 9:20





          How can I tell those cammands and CanExecute method, that the value inside my SpinEdit is incorrect? I haven't been able to find any solution to that problem. I find it hard to use the example you provided and the others I've found in my situation.

          – bananeeek
          Nov 16 '18 at 9:20













          @bananeeek - The CanExecute function gets called by WPF when anything changes, and should be able to check the value of your controls.

          – Robin Bennett
          Nov 16 '18 at 9:38





          @bananeeek - The CanExecute function gets called by WPF when anything changes, and should be able to check the value of your controls.

          – Robin Bennett
          Nov 16 '18 at 9:38













          Personally I recommend that you don't use the old style CommandManager as you have no control over when CanExecute is called (perhaps after every key stroke or mouse click). Instead you should use an implementation of ICommand that requires you to explicitly call CanExecuteChanged as required.

          – Peregrine
          Nov 16 '18 at 9:45





          Personally I recommend that you don't use the old style CommandManager as you have no control over when CanExecute is called (perhaps after every key stroke or mouse click). Instead you should use an implementation of ICommand that requires you to explicitly call CanExecuteChanged as required.

          – Peregrine
          Nov 16 '18 at 9:45



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333788%2fhow-to-allow-pressing-only-the-cancel-button-when-the-value-is-invalid%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党