Select to view content in your preferred language

Uncheck checkboxes on a Custom Control from another form.

857
6
Jump to solution
08-30-2023 01:35 PM
IanBaca2
New Contributor II

I have a plugin that streams imagery and basmaps from proprietary apis. This has Login/Logout functionality. Logout should reset everything to the state before login (i.e. checkboxes should be unchecked and combo box value set back to default value). In this addin I have a split button on the ribbon with a custom control for the dropdown. This custom control has 4 checkboxes. I need to uncheck any checked boxes at logout. I am having difficulty achieving this. How do I access the checkboxes on a custom control from another form.

I have set up public and private bools in the custom control View Model as shown below.

 

private bool _isSelectedPremium;
public bool IsSelectedPremium
{
	get { return _isSelectedPremium; }
        set
        {
            _isSelectedPremium = value;
            NotifyPropertyChanged("IsSelectedPremium");
        }
}

 

 The public bool is bound to the check box in the xaml:

 

<CheckBox Name="PremiumChkBx" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,10,0" IsChecked="{Binding IsSelectedPremium}"/>

 

 I am able to do the business logic I need when checked on or off.  

But I cannot get the checkboxes to reset (uncheck if checked) at logout.  The Logout is a button click event handled in another form.

Any help is appreciated, thanks.

0 Kudos
1 Solution

Accepted Solutions
CharlesMacleod
Esri Regular Contributor

If u want to change the property from the UI just change the definition of your property in the view model to be get and set - and make sure the binding is Two-way (which is the default)...so delete out the binding mode stuff (in your xaml) that u added for one-way.

u want something like this in the vm....anyway, keep whittling away at it - sounds like u r almost there 😉

internal class CustomControl1ViewModel : CustomControl  {
   //public bool IsSelectedPremium => Module1.Current.IsSelectedPremium;
    public bool IsSelectedPremium {
      get => Module1.Current.IsSelectedPremium;
      set
      {
        if (value != Module1.Current.IsSelectedPremium)
        {
          Module1.Current.IsSelectedPremium = value;
          NotifyPropertyChanged();
        }
      }
    }

 

View solution in original post

0 Kudos
6 Replies
CharlesMacleod
Esri Regular Contributor

move the backing properties to the Module. The module is accessible from anywhere in the addin. Add a "refresh" method to your custom control view model that fires the property notification.

 

internal class Module1 : Module {

    private static Module1 _this = null;

    public bool IsSelectedPremium { get; set; }

internal class CustomControlViewModel1 : CustomControl {

    public bool IsSelectedPremium => Module1.Current.IsSelectedPremium;

    public void Refresh() {
      NotifyPropertyChanged("IsSelectedPremium");
    }


internal class SomeOtherClassElseWhereInTheAddin {

    public void SetCheckboxes(bool loggedIn) {
       Module1.Current.IsSelectedPremium = loggedIn;
       var custom_control_vm = 
          FrameworkApplication.GetPlugInWrapper("Acme_Custom_Control1")
               as CustomControlViewModel1;
       custom_control_vm.Refresh();
    }

        
0 Kudos
IanBaca2
New Contributor II

Hello and thanks for the response. I have set up the code as outlined above. However, I am still unable to get this to work. The GetPlugInWrapper("string id") call is returning null so the call to .Refresh() is causing a crash. I see from the documentation that GetPlugInWrapper takes the id of the DAML element. So where you have "Acme_Custom_Control1" I have the id of the custom control from the DAML "X_CustomControl".

Custom Control in config.Daml:

 

<customControl id="X_CustomControl" keytip="Products" className="CustomControlViewModel" loadOnClick="true" 
                       smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonPurple16.png" 
                      largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonPurple32.png">
          <content className="CustomControlView" />
          <tooltip heading="Products">Choose.<disabledText /></tooltip>
        </customControl>

 

So my code looks like:

 

public void SetCheckboxes(bool loggedIn)
        {
            Module1.Current.IsSelectedPremium = loggedIn;
            var custom_control_vm =
               ArcGIS.Desktop.Framework.FrameworkApplication.GetPlugInWrapper("X_CustomControl")
                    as CustomControlViewModel;
            custom_control_vm.Refresh();
        }

 

But my custom_control_vm returns null (even after I've opened it and checked a box).

Am I misunderstanding the GetPlugInWrapper string parameter or does this not recognize Custom Controls? 

Thanks again.

0 Kudos
CharlesMacleod
Esri Regular Contributor

yes, sorry about that. the cast wont work. i forgot that the wrapper cannot be cast to the view model directly. The view model "itself" is held in an internal property.

Change the code like so:

 

 

internal class Module1 : Module  {
   //Add another property to your module for the custom control
    public CustomControlViewModel1 CustomControlViewModel1  { get; set;}

internal class CustomControlViewModel1 : CustomControl {
    //add a default ctor
    public CustomControlViewModel1() {
      //set it here in the ctor of your custom control vm
      Module1.Current.CustomControlViewModel1 = this;
    }

//call refresh on the module property - add a null check
//if CustomControlViewModel1 -is- null it means that it has not been
//instantiated yet - presumably because the ribbon tab it is sited on
//has never been activated in the session...in which case it does not
//need to be refreshed.
Module1.Current.CustomControlViewModel1?.Refresh();

 

 

0 Kudos
IanBaca2
New Contributor II

We are getting closer I think.  With the above change I can get the checkbox to uncheck at the logout function. However, now it seems the binding for setting the bool to true when clicking to check the checkbox is lost. At first changing my code to what was suggested above, I was given the error "A TwoWay or OneWayToSource binding cannot work on the read-only property 'IsSelectedPremium' of type 'System.String'." So I had to change the Binding Mode to OneWay specifically to run this (I don't know if that is a factor). 

Thanks again for your time.

0 Kudos
CharlesMacleod
Esri Regular Contributor

If u want to change the property from the UI just change the definition of your property in the view model to be get and set - and make sure the binding is Two-way (which is the default)...so delete out the binding mode stuff (in your xaml) that u added for one-way.

u want something like this in the vm....anyway, keep whittling away at it - sounds like u r almost there 😉

internal class CustomControl1ViewModel : CustomControl  {
   //public bool IsSelectedPremium => Module1.Current.IsSelectedPremium;
    public bool IsSelectedPremium {
      get => Module1.Current.IsSelectedPremium;
      set
      {
        if (value != Module1.Current.IsSelectedPremium)
        {
          Module1.Current.IsSelectedPremium = value;
          NotifyPropertyChanged();
        }
      }
    }

 

0 Kudos
IanBaca2
New Contributor II

This worked. I just had to specifically set the bool to false in the Refresh() method. 

Thanks so much!

0 Kudos