Search code examples
c#propertieswinrt-xamlrelaycommandisenabled

How to set boolean flag based on multiple property selections?


Overview: I've bound the IsEnabled property of a button to a bool flag in it's related view model.

In the current UI setup the IsValidTagRequest should be set to true if all three properties have a selection, i.e RaisePropertyChanged() has fired in each setter.

What I did try is setting the private bool _isValidTagRequest to true after the RaisePropertyChanged() of each of the required properties - RegNumber, SelectedZone, SelectedParkDuration.

But this scenario doesn't account for all three properties having a value selected to enable the button.

Question: Does anyone know how I can check that multiple properties have been changed before enabling a bool property to true?

The bool proeprty is defined as follows, followed by each of the required properties to be changed before IsEnabled is set to true:

    private bool isValidTagRequest = false;
    public bool IsValidTagRequest
    {
        get { return isValidTagRequest; }
        set
        {
           if (value != isValidTagRequest)
           {              
                isValidTagRequest = value;
                RaisePropertyChanged("IsValidTagRequest");
           }

        }

    }

Three properties - RegNumber, SelectedZone, SelectedParkDuration:

    private string _regNumber;
    public string RegNumber
    {
        get
        {
            return this._regNumber;
        }

        set
        {
            if (_regNumber != value)
            {
                _regNumber = value;
                RaisePropertyChanged("RegNumber");
            }
        }
    }

    private ZoneInfo _selectedZone;
    public ZoneInfo SelectedZone
    {
        get
        {
            return this._selectedZone;
        }

        set
        {
            if (_selectedZone != value)
            {
                _selectedZone = value;
                RaisePropertyChanged("SelectedZone");
            }
        }
    }


    private TimeSpan? _selectedParkDuration = TimeSpan.Parse("00:00");
    public TimeSpan? SelectedParkDuration
    {
        get
        {
            return this._selectedParkDuration;
        }

        set
        {
            if (_selectedParkDuration != value)
            {
                _selectedParkDuration = value;
                RaisePropertyChanged("SelectedParkDuration");
            }
        }
    }

And for context, the binding in XAML is defined as follows:

<Button Grid.Row="3"
                    Grid.Column="1"
                    Width="200"
                    Height="100"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Bottom"
                    Command="{Binding TagRequestCommand}"
                    Content="Send"
                    IsEnabled="{Binding IsValidTagRequest,
                                        Mode=TwoWay}"
                    Style="{StaticResource CustomButtonStyle}" />

Solution

  • For example, you could call RaisePropertyChanged("IsValidTagRequest"); at the end of each setter of your 3 properties. So when one of theses properties are set, the binding of IsValidTag would be reevaluated. Though you should update the code of the getter of "IsValidTagRequest" to return the combination of the 3 properties, something like:

    public bool IsValidTagRequest
    {
        get { return SelectedParkDuration != null && SelectedZone != 0 & RegNumber != "" };
    }
    

    Please mark as answer if that helps, thank you Stéphanie