简体   繁体   中英

How to fire the Combobox Selection Changed event if there's only 1 entry in the drop down

I have a Combobox in WPF and I am using MVVM pattern. When the combobox item selection is changed, it fires an event that does a few things. But if there's only 1 entry in the dropdown, it fires the event for the first time I select it. After that, if I select it again, it won't call the SelectionChanged Event. Is there any way to do this?

Here's my code:

  <ComboBox x:Name="DataComboBox" MinWidth="125" Text="" Margin="5,3" VerticalAlignment="Center"  Grid.Row="8" Grid.Column="1" Style="{StaticResource ComboBoxFlatStyle}"
    IsEditable="True" IsReadOnly="True" ItemsSource="{Binding ComboBoxList}" DisplayMemberPath="Scan_File_Name" SelectedItem="{Binding SelectedItems}"></ComboBox>

    private string selectedItem;

    public string SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (value != selectedItem)
            {
                selectedItem= value;
                OnPropertyChanged("SelectedItem");
                SelectedItemsChanged();
            }

        }
    }


    private void SelectedImagesChanged()
    {
          //Do some work
    }

In WPF, the selection changed event only happens when the value changes. Dropping down the combo box and clicking on the same entry will not change the selected entry. Therefore no event is fired.

There are other ways to do what you want but it's unlikely they are needed.
One can catch the click event for clicking on an entry in the combobox and process it the same as a selection changed
If it is needed to refresh data on the form, one can add a REFRESH DATA button instead.

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