-1

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.

  • 1
    Set the `IsEnabled` property of the `ComboBox` itself or a source property to`false` just before you start the long-running process and then set it back to `true` in a `finally` block? – mm8 Aug 19 '21 at 13:41

1 Answers1

2

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.

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • This worked perfectly. Now when the combo box is enabled at the end of the process, the focus gets lost from the combo box. Should I set GotFocus = true to regain the focus? Or are there some better attribute for focus? – Abhishek Kumar Aug 26 '21 at 19:35
  • can you suggest me some ideas on how to get the focus back to combo box after it gets enabled? I tried Focusable and gotFocus but apparently it is not working. – Abhishek Kumar Aug 27 '21 at 19:39
  • 1
    There is a method called .Focus() which you need to call. GotFocus is an event which is triggered when you recieve focus – Anu Viswan Aug 28 '21 at 01:29
  • Hi , I could not find any .Focus() for combobox. There is no code behind. Am I missing something here? Please guide me. – – Abhishek Kumar Aug 30 '21 at 07:49