简体   繁体   English

如果下拉菜单中只有 1 个条目,如何触发 Combobox 选择更改事件

[英]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.我在 WPF 中有一个 Combobox,我正在使用 MVVM 模式。 When the combobox item selection is changed, it fires an event that does a few things.当 combobox 项目选择发生更改时,它会触发一个执行一些操作的事件。 But if there's only 1 entry in the dropdown, it fires the event for the first time I select it.但是,如果下拉列表中只有 1 个条目,它会第一次触发该事件我 select 它。 After that, if I select it again, it won't call the SelectionChanged Event.之后,如果我再次 select 它,它就不会调用 SelectionChanged 事件。 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.在 WPF 中,选择更改事件仅在值更改时发生。 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可以捕获单击 combobox 中的条目的单击事件,并像选择更改一样处理它
If it is needed to refresh data on the form, one can add a REFRESH DATA button instead.如果需要刷新表单上的数据,可以添加一个 REFRESH DATA 按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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