Search code examples
c#wpfmvvmmodel-bindingasp.net-mvc-viewmodel

How do I disable and enable comboBox in WPF based on some other process?


I have a situation in which I have to dither combo box while some process is being completed.

This snippet is of the Combo Box XAML (ItemSearchView.XAML)..

    <StackPanel Margin="2.5">
        <Label Content="{x:Static local:StringResources.LBL_FILL_LOC}" Target=" 
        {Binding ElementName=CboFillLoc}"/>
        <ComboBox x:Name="CboFillLoc" IsEnabled="{Binding IsComboEnabled}"
         ItemsSource="{Binding Locations}" SelectedItem="{Binding 
         SelectedLocation, Mode=TwoWay}" Padding="2.5"/>
    </StackPanel>

This is the method where combo box need to dithered at the beginning and needs to be enabled at the end.


    private async void FetchItems()
    {
        try
        {
             do something
        }
        catch
        {
    
        }
        finally
        {
    
        }
    
    }

The purpose for this is that whenever a user is searching for the items manually, I have to restrict the user to perform other processes until all the items are loaded properly.

I am not able to achieve this since I am very new to WPF. Any suggestions or help will be highly appreciable.


Solution

  • Hope I understood you issue correctly. You could declare a boolean property and bind it to IsEnabled Property of the ComboBox.

    For example,in your ViewModel

    private bool _isComboEnabled;
    public bool IsComboEnabled
    {
      get=>_isComboEnabled;
      set
      {
        if(_isComboEnabled==value) return;
        _isComboEnabled = value;
        NotifyOfPropertyChanged();
      }
    }
    

    In Xaml, you could now

    <ComboBox x:Name="cboFillLoc" IsEnabled={Binding IsComboEnabled}.../>
    

    Now each time you want to invoke the SearchItemManually method, you could ensure the ComboEnabled flag is turned off and turn it on again at the end of the method.