0

I have a combo box in the XAML written as

<ComboBox  ItemsSource="{Binding Path=Options}" 
           SelectedItem="{Binding Path=CurrentValue}"/>

And the "CurrentValue" is implemented in the ViewModel class as

private string m_CurrentValue;
public string CurrentValue
{
    get { return this.m_CurrentValue; }
    set
    {
        if (m_CurrentValue != value)
        {
            if (IsValid(value))
            {
                this.m_CurrentValue = value;
                SetData(this.m_CurrentValue);
            }

            this.SendPropertyChangedEvent(nameof(this.CurrentValue));
        }
    }
}

Here before setting CurrentValue, it is validated for some condition. My intention is to change ComboBox box selection to the previous value if the validation fails. This is not working for combo-boxes, however this method works perfectly for CheckBox controls - code snippet given below.

<CheckBox VerticalAlignment="Center" IsChecked="{Binding Path=CurrentValue}" Width="15" IsEnabled="{Binding Path=IsEnabled}"/>

private bool m_CurrentValue;
public bool CurrentValue
{
    get { return this.m_CurrentValue; }
    set
    {
        if (m_CurrentValue != value)
        {
            if (IsValid(value))
            {
                this.m_CurrentValue = value;
                SetData(this.m_CurrentValue);
            }

            this.SendPropertyChangedEvent(nameof(this.CurrentValue));
        }
    }
} 

Is there any way to make this work for ComboBox? Any alternate implementation is also fine.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
FaisalM
  • 724
  • 5
  • 18
  • What happens if you make the selecteditem binding twoway? – Andy Dec 03 '18 at 13:32
  • @Andy same behavior – FaisalM Dec 03 '18 at 13:37
  • 2
    @FaisalM I think the better approach would be using a `ValidationRule` on your `Binding` or fill your `ComboBox` only with valid items. Because i think from user perspective its always bad if an entered value changes without any feedback. – nosale Dec 03 '18 at 13:47

2 Answers2

1

Is there any way to make this work for ComboBox? Any alternate implementation is also fine.

You could set the backing field to the previous value using the dispatcher as suggested here:

private string m_CurrentValue;
public string CurrentValue
{
    get { return this.m_CurrentValue; }
    set
    {
        if (m_CurrentValue != value)
        {
            string previousValue = m_CurrentValue;
            //set the field
            this.m_CurrentValue = value;
            if (IsValid(value))
            {
                SetData(this.m_CurrentValue);
                this.SendPropertyChangedEvent(nameof(this.CurrentValue));
            }
            else
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    m_CurrentValue = previousValue;
                    this.OnPropertyChanged(nameof(this.CurrentValue));
                }), DispatcherPriority.ApplicationIdle);
            }
        }
    }
}

Please refer to the link for more information.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

Create a user control with a combobox within it which will handle your needs. Have dependency properties which you can track and if need be change due state values you have setup to process the operations within the contained combobox.

Create an internal state variable which has the index of the current combo selection. When an operation detects a return that previous state, set the internal combobox to that value.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122