Select to view content in your preferred language

Property "IsEnabled" is not changed in ProWindow

1962
4
Jump to solution
10-18-2021 04:48 AM
BarbaraSchneider2
Frequent Contributor

I created a check box and a combo box in a ProWindow. In the xaml file, this is defined as follows:

 

<CheckBox x:Name="checkBoxSchool" IsChecked="{Binding SchoolChecked}" Content="School"/>
<ComboBox x:Name="comboBoxSchool" ItemsSource="{Binding SchoolItems}" SelectedItem="{Binding SelectedSchoolItem}" IsEnabled="{Binding SchoolComboEnabled}"/>

 

 When the ProWindow is opened, the check box should be disabled. The code in the view model is:

 

private void InitViewModel()
{
    SchoolComboEnabled = false;
}

public bool SchoolComboEnabled
{
    get { return _schoolComboEnabled; }
    set
    {
        _schoolComboEnabled = value;
        OnPropertyChanged();
    }
}

protected void OnPropertyChanged([CallerMemberName] string name = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}

 

This works.

When the user checks the check box, the combo box should become enabled:

 

public bool SchoolChecked
{
    get { return _schoolChecked; }
    set
    {
         _schoolChecked= value;
        if (_schoolChecked)
        {
            SchoolComboEnabled = true;
            OnPropertyChanged();
        }
    }
}

 

This doesn't work, i.e. the check box doesn't become enabled.

I tried the exact same code in a dock pane, and it worked! The problem is that I need a window and not a dock pane.
I also created a user control that is embedded in a ProWindow (see ReusableUserControl ) but this doesn't work neither.

Any help is appreciated!

0 Kudos
2 Solutions

Accepted Solutions
BarbaraSchneider2
Frequent Contributor

I found out what I missed: I have to specify that my view model class implements the interface INotifyPropertyChanged:

public class WindowEducationPointsViewModel : INotifyPropertyChanged

View solution in original post

Wolf
by Esri Regular Contributor
Esri Regular Contributor

PropertyChangedBase is a convenience base class that implements INotifyPropertyChanged.  It's the same base class that is also used by DockPane and Pane (you have to follow the inheritance of these classes to find PropertyChangedBase).  If you derive your ProWindowViewModel class from PropertyChangedBase like this:

public class ProWindowDialogVM : PropertyChangedBase

You can now take advantage of convenience methods in PropertyChangedBase that implement INotifyPropertyChanged. For example, i can implement a public property called UserName like this:

private string _userName;
public string UserName
{
    get { return _userName; }
    set
    {
      SetProperty(ref _userName, value, () => UserName);
    }
}

SetProperty is implemented by PropertyChangedBase which in turn implements INotifyPropertyChanged.  So if I use this snippet to set the UserName property:

UserName = "My Name";

 the SetProperty method will automatically raise the required property changed notification.  I can also raise a notification changed event manually by calling:

NotifyPropertyChanged(() => UserName);

 You will find SetProperty and NotifyPropertyChanged throughout our samples, that's the reason why the view model in the sample above derives from PropertyChangedBase.

View solution in original post

4 Replies
BarbaraSchneider2
Frequent Contributor

I found out what I missed: I have to specify that my view model class implements the interface INotifyPropertyChanged:

public class WindowEducationPointsViewModel : INotifyPropertyChanged
Wolf
by Esri Regular Contributor
Esri Regular Contributor
BarbaraSchneider2
Frequent Contributor

Thank you Wolf. In the sample you mention, the view model class inherits from PropertyChangedBase:

public class ProWindowDialogVM : PropertyChangedBase

However, in order to change the enabled or checked property of a GUI component programmatically, the view model class has to implement INotifyPropertyChanged.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

PropertyChangedBase is a convenience base class that implements INotifyPropertyChanged.  It's the same base class that is also used by DockPane and Pane (you have to follow the inheritance of these classes to find PropertyChangedBase).  If you derive your ProWindowViewModel class from PropertyChangedBase like this:

public class ProWindowDialogVM : PropertyChangedBase

You can now take advantage of convenience methods in PropertyChangedBase that implement INotifyPropertyChanged. For example, i can implement a public property called UserName like this:

private string _userName;
public string UserName
{
    get { return _userName; }
    set
    {
      SetProperty(ref _userName, value, () => UserName);
    }
}

SetProperty is implemented by PropertyChangedBase which in turn implements INotifyPropertyChanged.  So if I use this snippet to set the UserName property:

UserName = "My Name";

 the SetProperty method will automatically raise the required property changed notification.  I can also raise a notification changed event manually by calling:

NotifyPropertyChanged(() => UserName);

 You will find SetProperty and NotifyPropertyChanged throughout our samples, that's the reason why the view model in the sample above derives from PropertyChangedBase.