繁体   English   中英

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

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

我在 WPF 中有一个 Combobox,我正在使用 MVVM 模式。 当 combobox 项目选择发生更改时,它会触发一个执行一些操作的事件。 但是,如果下拉列表中只有 1 个条目,它会第一次触发该事件我 select 它。 之后,如果我再次 select 它,它就不会调用 SelectionChanged 事件。 有没有办法做到这一点?

这是我的代码:

  <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
    }

在 WPF 中,选择更改事件仅在值更改时发生。 下拉组合框并单击同一条目不会更改所选条目。 因此不会触发任何事件。

还有其他方法可以做你想做的事,但不太可能需要它们。
可以捕获单击 combobox 中的条目的单击事件,并像选择更改一样处理它
如果需要刷新表单上的数据,可以添加一个 REFRESH DATA 按钮。

暂无
暂无

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

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