[英]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.