确定事件来源

发布于 2024-11-30 19:16:19 字数 617 浏览 0 评论 0原文

我有一个下拉列表和单选按钮。如果用户从下拉列表中选择了某些内容,我希望清除单选按钮。如果选择了单选按钮,我希望清除下拉列表的选择。不幸的是,这会产生相互抵消的事件。我尝试使用如下所示的发送者来确定该值是否被代码或用户更改,但这不起作用。如何使这些事件仅在用户是操作源时才起作用?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贵在坚持 2024-12-07 19:16:19

您将无法区分两者之间的区别,因为这两种情况下的源都是相同的实例。

这并不能直接回答问题,但如果您在 SelectionChanged 事件处理程序中比较comboBoxTitles的SelectedIndex,您的问题应该得到解决

private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (comboBoxTitles.SelectedIndex != -1)
    {
        rbBlank.IsChecked = false;
    }
}

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 the SelectionChanged event handler, your problem should be solved

private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (comboBoxTitles.SelectedIndex != -1)
    {
        rbBlank.IsChecked = false;
    }
}
清醇 2024-12-07 19:16:19

尝试比较 sender == 控件的实例而不是 is 的类型。

private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
    // Verify source of event
    if (sender == rbBlank)
    {
        // Display
        comboBoxTitles.SelectedIndex = -1;
    }
}


private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    // Verify source of event
    if (sender == comboBoxTitles)
    {
        // Display
        rbBlank.IsChecked = false;
    }
}

Try to compare if sender == instance of a control instead of is type of.

private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
    // Verify source of event
    if (sender == rbBlank)
    {
        // Display
        comboBoxTitles.SelectedIndex = -1;
    }
}


private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    // Verify source of event
    if (sender == comboBoxTitles)
    {
        // Display
        rbBlank.IsChecked = false;
    }
}
小红帽 2024-12-07 19:16:19

如果您知道这些控件的 ID,您可以尝试这样的操作:

System.Web.UI.WebControls.WebControl webControl = (System.Web.UI.WebControls.WebControl) sender;

if( webControl.ID == <comboboxId>)
{
//Do something
}

我还没有尝试过这个,但我想它可能会起作用。

If you know the IDs of those controls, you can try something like this:

System.Web.UI.WebControls.WebControl webControl = (System.Web.UI.WebControls.WebControl) sender;

if( webControl.ID == <comboboxId>)
{
//Do something
}

I havent tried this, but I guess it might work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文