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.