Select to view content in your preferred language

How do I get the bool values of ProWindow Radiobuttons and Checkboxes in the MVVM ViewModel?

1042
3
Jump to solution
09-06-2022 04:32 PM
RichardFairhurst
MVP Honored Contributor

I am having trouble understanding how to implement RadioButons and CheckBoxes using MVVM.  It seems that Binding the IsChecked property to a bool get/set ViewModel property doesn't work.  What is the trick?

Here is my xaml set up for the radio buttons:

 

<StackPanel x:Name="StpMatchType" HorizontalAlignment="Left" Height="65" Margin="19,114,0,0" VerticalAlignment="Top" Width="134">
            <RadioButton x:Name="rbStart" GroupName="Test" Content="Start of Name" Height="20" IsChecked="{Binding Path=RBStart,Mode=TwoWay}"/>
            <RadioButton x:Name="rbAnyPart" GroupName="Test" Content="Any Part of Name" Height="20" IsChecked="{Binding Path=RBAny,Mode=TwoWay}"/>
            <RadioButton x:Name="rbExact" GroupName="Test" Content="Exact Name" Height="20" IsChecked="{Binding Path=RBExact,Mode=TwoWay}"/>
        </StackPanel>
<CheckBox x:Name="CbxTitle" Content="Set Map Title" HorizontalAlignment="Left" Margin="19,203,0,0" VerticalAlignment="Top" Height="15" Width="90" IsChecked="{Binding SetTitle}"/>
        

 

 

 

And here are the ViewModel property set up, but this code is never triggered at set up or when I click on one of the radiobuttons:

 

        private bool _rbStart = true;
        private bool RBStart
        {
            get { return _rbStart; }
            set
            {
                _rbStart = value;
                NotifyPropertyChanged(() => RBStart);
            }
        }
        private bool _rbAny = false;
        private bool RBAny
        {
            get { return _rbAny; }
            set
            {
                _rbAny = value;
                NotifyPropertyChanged(() => RBAny);
            }
        }
        private bool _rbExact = false;
        private bool RBExact
        {
            get { return _rbExact; }
            set
            {
                _rbExact = value;
                NotifyPropertyChanged(() => RBExact);
            }
        }
        public bool SetTitle
        {
            get { return Module1.SetTitle; }
            set
            {
                Module1.SetTitle = value;
                NotifyPropertyChanged(() => SetTitle);
            }
        }

//Module1 Code
        private static bool _setTitle;

        public static bool SetTitle
        {
            get { return _setTitle; }
            set
            {
                _setTitle = value;
            }
        }

 

It seems like all of the Google examples are overly complex and solving value conversion problems that don't apply to me.  Even my code seems like overkill, since the behavior of the set of radiobuttons and checkboxes works fine while it ignores my code.  I don't want to modify the default behavior of the radiobuttons or checkboxes at all and I don't really have to be notified or do anything when the user actually changes the radiobutton selection or checkbox state.  I just need to be able to access the bool state of these buttons in my ViewModel when I trigger some of my other methods.  If I can do that without actually needing to bind any of the radiobuttons or checkboxes, that would be great.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

i am checking right now, but for sure your view model properties have to be declared as public properties:

public bool RBStart

 

 

View solution in original post

0 Kudos
3 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

i am checking right now, but for sure your view model properties have to be declared as public properties:

public bool RBStart

 

 

0 Kudos
RichardFairhurst
MVP Honored Contributor

Also, the BindingContext is set up properly in my xaml file since my bindings and code are working property and giving change notifications from my textboxes, buttons and listboxes in response to the get and set code of the properties I set up for them.  Only the radiobuttons and checkboxes bindings and notifications are not working.

 

However, you are right that I copy/pasted properties that were set to private and didn't notice it.  Changing it to public solved the issue.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Also, if you search for 'RadioButton' in our Pro SDK community samples you will find a few implementations that you can look at for example this solution is using RadioButton controls with code-behind:

Esri/arcgis-pro-sdk-community-samples: ArcGIS Pro SDK for Microsoft .NET Framework Community Samples...

 

0 Kudos