确定事件来源
我有一个下拉列表和单选按钮。如果用户从下拉列表中选择了某些内容,我希望清除单选按钮。如果选择了单选按钮,我希望清除下拉列表的选择。不幸的是,这会产生相互抵消的事件。我尝试使用如下所示的发送者来确定该值是否被代码或用户更改,但这不起作用。如何使这些事件仅在用户是操作源时才起作用?
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender is RadioButton)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender is ComboBox)
{
// Display
rbBlank.IsChecked = false;
}
}
I have a dropdown list and radio button. If something is selected from the dropdown by the user, I want the radio button cleared. If the radio button is selected I want the selection of the dropdown cleared. Unfortunately, this creates events that cancel each other out. I tried using the sender as shown below to determine if the value was being changed by code or by the user, but that doesn't work. How do I make these events only work if the user is the source of the action?
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender is RadioButton)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender is ComboBox)
{
// Display
rbBlank.IsChecked = false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将无法区分两者之间的区别,因为这两种情况下的源都是相同的实例。
这并不能直接回答问题,但如果您在
SelectionChanged
事件处理程序中比较comboBoxTitles的SelectedIndex
,您的问题应该得到解决You won't be able to tell the difference between the two since the source will be the same instance for both occasions.
This doesn't answer the question directly but if you compare the
SelectedIndex
of comboBoxTitles in theSelectionChanged
event handler, your problem should be solved尝试比较 sender == 控件的实例而不是 is 的类型。
Try to compare if sender == instance of a control instead of is type of.
如果您知道这些控件的 ID,您可以尝试这样的操作:
我还没有尝试过这个,但我想它可能会起作用。
If you know the IDs of those controls, you can try something like this:
I havent tried this, but I guess it might work.