WPF - Fire comboBox SelectedIndexChanged from user only
I want the ComboBox_SelectionChanged event not to fire when the UI is loaded. This should only happen when the user makes some changes to the combo box.
To do this, I wrote the following in the .xam.cs file.
private void myComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb = (ComboBox)sender;
if (!cb.IsFocused)
{
return;
}
else
ViewModel.OnPropertyChanged("MyProperty");
}
But that doesn't even fire an event when the user makes a change. Where did I go wrong?
I know there is a similar question on stackoverflow. But the solutions in it didn't work for me. Pls help.
+3
source to share
2 answers
found a solution. we just need to combine the Selection_Changed event with the PreviewMouseDown event.
Can the SelectionChanged event in WPF be handled for user interaction only?
+2
source to share
This is a XAML example:
<av:ComboBox x:Name="cmbChargeUnit" HorizontalAlignment="Left" Margin="548,15,0,0" Width="187" ItemsSource="{av:Binding ChargeUnits}" DisplayMemberPath="ChargeUnitDescription" SelectedValue="{Binding SelectedChargeUnit}" VerticalAlignment="Top" Background="#FFCBCBCB" Height="20" IsSynchronizedWithCurrentItem="True" SelectedIndex="0" BorderBrush="#FF7070CB"/>
VM:
public ChargeUnit SelectedChargeUnit
{
get { return _selectedChargeUnit; }
set
{
_selectedChargeUnit = value;
OnPropertyChanged("SelectedChargeUnit");
if (SelectedAttributeId != null)//Only Load when an attribute is entered
{
LoadRates();
}
}
}
0
source to share