简体   繁体   English

WPF - Fire comboBox SelectedIndexChanged仅来自用户点击

[英]WPF - Fire comboBox SelectedIndexChanged only from user click

I want to stop the ComboBox_SelectionChanged event from being fired at the UI loading. 我想停止在UI加载时触发ComboBox_SelectionChanged事件。 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. 为此,我在.xam.cs文件中编写了以下内容。

  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. 我知道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. 我们只需要将Selection_Changed事件与PreviewMouseDown事件相结合。

could the SelectionChanged event in WPF be handled only for user interaction? 是否可以仅为用户交互处理WPF中的SelectionChanged事件?

This is an example XAML: 这是一个示例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: VM:

public ChargeUnit SelectedChargeUnit
    {
        get { return _selectedChargeUnit; }
        set
        {
            _selectedChargeUnit = value;
            OnPropertyChanged("SelectedChargeUnit");
            if (SelectedAttributeId != null)//Only Load when an attribute is entered
            {
                LoadRates();
            }
        }
    }

暂无
暂无

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

相关问题 Fire comboBox SelectedIndexChanged仅来自用户点击 - Fire comboBox SelectedIndexChanged only from user click 无法为组合框中的单个项目触发SelectedIndexChanged事件 - Unable to fire SelectedIndexChanged Event for a single item in a combobox 点击按钮时触发SelectedIndexChanged事件 - Fire SelectedIndexChanged event on button click for dropdown 使用键盘操作时,仅当用户按下Enter时,才会在列表框上触发SelectedIndexChanged事件 - Fire SelectedIndexChanged event on a listbox, when operating with keyboard, only when user presses Enter 在用户控件内的ComboBox上调用SelectedIndexChanged - Call SelectedIndexChanged on ComboBox within User Control 以编程方式更改索引时,不会触发ComboBox.SelectedIndexChanged - ComboBox.SelectedIndexChanged does not fire when programatically changing the index Winforms:哪个事件要触发? combobox.selectedindexchanged或bindingsource.currentchanged - Winforms: Which event to fire? combobox.selectedindexchanged OR bindingsource.currentchanged 当我将值写入ComboBox时,不会触发SelectedIndexChanged事件。 当我从comboBox.Items列表中选择一个值时,它会触发 - SelectedIndexChanged event does not fire when I write a value into the ComboBox. It does fire when I select a value from the comboBox.Items list ComboBox SelectedIndexChanged - ComboBox SelectedIndexChanged 为什么我的SelectedIndexChanged事件不会在Button_Click中触发? - Why won't my SelectedIndexChanged event fire in Button_Click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM