简体   繁体   中英

WPF - Fire comboBox SelectedIndexChanged only from user click

I want to stop the ComboBox_SelectionChanged event from being fired at the UI loading. It should happen only when the user makes some change in the combo box.

To do this, I have written the following in .xam.cs file.

  private void myComboBox_SelectionChanged(object sender,   SelectionChangedEventArgs e)
      {
        ComboBox cb = (ComboBox)sender;
        if (!cb.IsFocused)
        {
            return;
        }
        else
           ViewModel.OnPropertyChanged("MyProperty");
     }

But this does not even fire the event when the user makes the change. Where have I gone wrong ?

I know there is a similar question in stackoverflow. But the solutions in it did not work for me. pls help.

found the solution. we just need to combine the Selection_Changed event with PreviewMouseDown event.

could the SelectionChanged event in WPF be handled only for user interaction?

This is an example XAML:

<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();
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM