【问题标题】:How can I trigger when I leave the ComboBox?离开 ComboBox 时如何触发?
【发布时间】:2021-10-22 00:19:19
【问题描述】:

所以我得到了一个 DataGrid,其中我得到了一个 DataGridTemplateColumn。在该模板列中,我在其中放置了一个组合框。 ComboBox 从我的 ViewModel 中的 ObservableCollections 中获取了他的数据。所以我的代码现在看起来像这样:

 <DataGridTemplateColumn Header="Status" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="cbStatus"
                                          ItemsSource="{Binding Path=ocLieferumfangStati, ElementName=vmLieferumfang}"
                                Width="100" HorizontalAlignment="Left" 
                                          DisplayMemberPath="Beschreibung"
                                          SelectedValue="{Binding Status}"
                                          SelectedValuePath="Status" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

但是我现在想做的是,当我离开 ComboBox 时,有一个 Event 触发什么。类似于 DataGrid 事件 CellEditEnding

我已经尝试过的是在我的 MVVM 中:

ocLieferumfangStati.CollectionChanged += (sender, e) =>
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                MessageBox.Show("ADD");
            }
            if (e.Action == NotifyCollectionChangedAction.Move)
            {
                MessageBox.Show("Move");
            }
            if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                MessageBox.Show("Remove");
            }
            if (e.Action == NotifyCollectionChangedAction.Replace)
            {
                MessageBox.Show("Replace");
            }
        };

但只有 删除操作 被触发。

我不能在 ComboBox 中写,我只能选择 ComboBox 中的项目。那么,当我离开 ComboBox 时,如何才能发出 MessageBox 呢?这样当我点击 ComboBox 时,就会触发 MessageBox。

【问题讨论】:

  • “离开”是指“失去焦点”? stackoverflow.com/questions/27820278/…
  • 我的意思是当我点击组合框时。
  • 是的,但“敲出”不是一个事件。当您执行此类操作时,会触发许多更具体的事件。
  • 也许您正在寻找 SelectionChanged?

标签: c# wpf mvvm datagrid


【解决方案1】:

我想你想要PreviewLostKeyboardFocusLostKeyboardFocus, 所以在这里检查Detect when a ListBoxItem is in the SelectedUnfocused stateWPF Combobox lostfocus event become an Infinite lopp

【讨论】:

  • 当我尝试打开 ComboBox 时触发
猜你喜欢
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 2013-04-02
  • 1970-01-01
  • 2018-03-13
相关资源
最近更新 更多