C# WPF-Fire组合框选择仅从用户单击更改索引

C# WPF-Fire组合框选择仅从用户单击更改索引,c#,wpf,combobox,C#,Wpf,Combobox,我想阻止在UI加载时触发ComboBox_SelectionChanged事件。只有当用户在组合框中进行一些更改时,才会发生这种情况 为此,我在.xam.cs文件中编写了以下内容 private void myComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBox cb = (ComboBox)sender; if (!cb.IsFocus

我想阻止在UI加载时触发ComboBox_SelectionChanged事件。只有当用户在组合框中进行一些更改时,才会发生这种情况

为此,我在.xam.cs文件中编写了以下内容

  private void myComboBox_SelectionChanged(object sender,   SelectionChangedEventArgs e)
      {
        ComboBox cb = (ComboBox)sender;
        if (!cb.IsFocused)
        {
            return;
        }
        else
           ViewModel.OnPropertyChanged("MyProperty");
     }
但这甚至不会在用户进行更改时触发事件。 我哪里出错了


我知道stackoverflow中也有类似的问题。但其中的解决方案对我并不适用。请提供帮助。

这是一个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"/>
找到了解决办法。 我们只需要将Selection_Changed事件与PreviewMouseDown事件结合起来


您是否遵循MVVM设计模式?如果是,为什么要处理SelectionChanged事件。如果此组合中有一个属性,我建议将Command属性绑定到ViewModel中的属性。Command属性不适用于组合框…对吗?是的,我查过了,它不在那里:(使用SelectedValue属性绑定到ViewModel中的属性它怎么会变为null?我有一个这样的属性。但它总是设置为某个值。只有当用户更改组合框中的项目时,它才会更改。该部分是在调用方法加载使用该属性的速率之前检查ViewModel中的另一个属性erty,就像我说的,这只是一个例子,当你在组合框中选择你的项目时,它会命中集合访问器,然后你可以调用一个每次该值更改时都会被调用的方法。我也已经尝试过了。这也是一样的。在页面加载时触发组合框选择更改事件。它不应该触发事件,因为uld不是一个事件?只是一个属性?当在组合中选择一个项目时,属性应该命中它的Set访问器。当窗口加载时,它将命中它的Get访问器。
public ChargeUnit SelectedChargeUnit
    {
        get { return _selectedChargeUnit; }
        set
        {
            _selectedChargeUnit = value;
            OnPropertyChanged("SelectedChargeUnit");
            if (SelectedAttributeId != null)//Only Load when an attribute is entered
            {
                LoadRates();
            }
        }
    }