Select to view content in your preferred language

mutual exclusion checkboxes

470
5
Jump to solution
08-21-2023 05:51 AM
Daniel4
New Contributor II

Hi

I want to implement two check boxes there are mutual exclusion. If box one are checked to true, box two must automatic turn to false. I have the two checkboxes in a dockpan.

Daniel4_0-1692621847030.png

This is what I have so far.

xaml:

 <CheckBox
    Margin="5"
    Style="{DynamicResource Esri_CheckboxToggleSwitch}"
    Name="CheckBox_One"
    IsChecked="{Binding IsCheckedBoxOne}">
    <TextBlock>
    <Run Text="BoxOne"/>
    </TextBlock>                
    </CheckBox>

<CheckBox
     Margin="5"
     Style="{DynamicResource Esri_CheckboxToggleSwitch}"
     Name="CheckBox_Two"
     IsChecked="{Binding IsCheckedBoxTwo}">
     <TextBlock>
     <Run Text="BoxTwo"/>
     </TextBlock>
</CheckBox>

c#:

private bool _isCheckedBoxOne;
private bool _isCheckedBoxTwo;

public bool IsCheckedBoxOne
       {
         get { return _isCheckedBoxOne; }
         set
            {               
                SetProperty(ref _isCheckedBoxOne, value);               
             }
         }        

public bool IsCheckedBoxTwo
        {
            get { return _isCheckedBoxTwo; }
            set
            {
               SetProperty(ref _isCheckedBoxTwo, value);
            }
        }

 

How to implement the logic?

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

If you implement your checkbox logic in the viewmodel as @GKmieliauskas  suggests you have to prevent recursion when you set the counter property.  Here is my suggestion (i attached a sample solution)

private bool _isCheckedOne;
    private bool _isCheckedTwo;

    public bool IsCheckedOne
    {
      get { return _isCheckedOne; }
      set
      {
        SetProperty(ref _isCheckedOne, value);
        if (IsCheckedTwo == IsCheckedOne)
        {
          // prevent recursion
          IsCheckedTwo = !IsCheckedOne;
        }
      }
    }

    public bool IsCheckedTwo
    {
      get { return _isCheckedTwo; }
      set
      {
        SetProperty(ref _isCheckedTwo, value);
        if (IsCheckedOne == IsCheckedTwo)
        {
          // prevent recursion
          IsCheckedOne = !IsCheckedTwo;
        }
      }
    }

View solution in original post

0 Kudos
5 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

There is no logic implemented in your code between IsCheckedOne and IsCheckedTwo properties.

private bool _isCheckedBoxOne;
private bool _isCheckedBoxTwo;

public bool IsCheckedBoxOne
       {
         get { return _isCheckedBoxOne; }
         set
            {         
                SetProperty(ref _isCheckedBoxOne, value);               
                _isCheckedBoxTwo = !_isCheckedBoxOne; 
                // Generate notification message here to update _isCheckBoxTwo, it could differ from that sample
               OnPropertyChanged("IsCheckedBoxTwo");
             }
         }        

public bool IsCheckedBoxTwo
        {
            get { return _isCheckedBoxTwo; }
            set
            {
               SetProperty(ref _isCheckedBoxTwo, value);
                _isCheckedBoxOne = !_isCheckedBoxTwo; 
                // Generate notification message here to update _isCheckBoxOne, it could differ from that sample
               OnPropertyChanged("IsCheckedBoxOne");
            }
        }

It is enough one property for both controls, but you need to use boolean inverting converter in second property binding.

Question like yours is not a scope of ArcGIS Pro. It is WPF part.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

If you implement your checkbox logic in the viewmodel as @GKmieliauskas  suggests you have to prevent recursion when you set the counter property.  Here is my suggestion (i attached a sample solution)

private bool _isCheckedOne;
    private bool _isCheckedTwo;

    public bool IsCheckedOne
    {
      get { return _isCheckedOne; }
      set
      {
        SetProperty(ref _isCheckedOne, value);
        if (IsCheckedTwo == IsCheckedOne)
        {
          // prevent recursion
          IsCheckedTwo = !IsCheckedOne;
        }
      }
    }

    public bool IsCheckedTwo
    {
      get { return _isCheckedTwo; }
      set
      {
        SetProperty(ref _isCheckedTwo, value);
        if (IsCheckedOne == IsCheckedTwo)
        {
          // prevent recursion
          IsCheckedOne = !IsCheckedTwo;
        }
      }
    }
0 Kudos
Daniel4
New Contributor II

Hi, thanks for the answer both of you. 

I try both, and it works for me. 

@Wolf I tried the sample solution, but its target AGO 3.2 🙂 

 

 

0 Kudos
Daniel4
New Contributor II

I changed the target in the daml-file and can run the souluton. 

Wolf
by Esri Regular Contributor
Esri Regular Contributor

The attached solution (should work with 3.0) has two options: one with a code-behind implementation and one with a XAML based implementation that is using a value converter.

 

0 Kudos