简体   繁体   English

显示消息框可防止IsChecked触发

[英]Displaying a MessageBox prevents IsChecked from firing

I have a few TextBoxes that are bound to a single CheckBox . 我有几个TextBoxes绑定到一个单一的CheckBox

This is the logic associated with it: 这是与之相关的逻辑:

  • If the Checkbox is checked, it will overwrite any existing text in the associated TextBoxes and make it '0' ++ 如果选中此Checkbox ,它将覆盖关联的文本TextBoxes所有现有文本,并将其设置为“ 0” ++
  • If all of the TextBoxes have a score of '0,' the CheckBox should become disabled and checked. 如果所有TextBoxes的得分都为“ 0”,则CheckBox应该被禁用并选中。
  • If any of the TextBoxes then change from '0,' it will become enabled and unchecked. 如果任何TextBoxes从'0'更改,它将变为启用状态且未选中。

++ * Note: * The caveat to this is if the TextBox has a value of 'C.' ++ * 注意: *需要注意的是,如果TextBox的值为'C'。

Okay, so the issue I have is implementing the caveat above when one of the associated TextBoxes has a value of 'C.' 好的,所以当关联的TextBoxes之一的值为'C'时,我遇到的问题是实施上述警告。 What I would like to have happen is loop through the TextBoxes and check to see if any are scored 'C.' 我想发生的事情是循环遍历TextBoxes并检查是否有任何得分为“ C”。 If one is found, display a warning message to the user confirming if they want to proceed. 如果找到一个,则向用户显示警告消息,确认他们是否要继续。 If Yes is selected, all associated scores will be overwritten to '0.' 如果选择“是”,则所有关联的分数将被覆盖为“ 0”。 If No is selected then the Checked event should be cancelled. 如果选择“否”,则应取消“已Checked事件。

To accomplish this, I added Event Listeners for the CheckBox.PreviewMouseDown and CheckBox.Checked events. 为此,我为CheckBox.PreviewMouseDownCheckBox.Checked事件添加了事件监听器。 Here is the code for my CheckBox.PreviewMouseDown event listener: 这是我的CheckBox.PreviewMouseDown事件侦听器的代码:

private void NormalCheckBoxControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    CheckBox normalCheckBox = (CheckBox)sender;
    bool showCorrespondingScoreWarningMsg = false;

    //Get a Row to loop through the AssociatedAdqScore controls for each
    ScoreControl tempScoreControl = new ScoreControl();

    foreach (ScoreControl score in this.ScoreControlList)
    {
        if (score.ScoreTextBox.Text == "C")
        {
            showCorrespondingScoreWarningMsg = true;
        }
    }

    if (showCorrespondingScoreWarningMsg)
    {
        MessageBoxResult msgResult = InformationBox.Show("WARNING: Proceeding will remove corresponding 'C' scores.  Continue?", "Continue?", ButtonStyle.YesNo, IconStyle.Question);

        if (msgResult == MessageBoxResult.No)
        {
            e.Handled = true;
        }
    }
}

This works if the user selects 'No,' however the issue I'm having is that when choosing 'Yes,' the CheckBox.Checked event still does not get fired. 如果用户选择“否”,则此方法有效,但是我遇到的问题是,当选择“是”时, CheckBox.Checked事件仍然不会触发。 I have tried to manually set CheckBox.IsChecked = true; 我试图手动设置CheckBox.IsChecked = true; if if (msgResult == MessageBoxResult.Yes) , but this breaks the bindings so that is not a viable solution. if if (msgResult == MessageBoxResult.Yes) ,但这会破坏绑定,因此这不是可行的解决方案。

Is there any way I can resolve this issue and proceed with the NormalCheckBoxControl_Checked(object sender, RoutedEventArgs e) event if the user selects 'Yes?' 如果用户选择“是”,有什么办法可以解决此问题并继续NormalCheckBoxControl_Checked(object sender, RoutedEventArgs e)事件?

I don't know of a "Checked" event, though there is a CheckedChanged event and also a CheckStateChanged event. 我不知道“已检查”事件,尽管有一个CheckedChanged事件和一个CheckStateChanged事件。 I am used to .NET 2.0, so this may be a 3.0+ thing. 我已经习惯了.NET 2.0,所以这可能是3.0+的东西。 Either way, I think I have manually called the event handler in instances like this without any problem. 无论哪种方式,我都认为在这样的实例中我手动调用了事件处理程序,没有任何问题。

You can manually call the event handler with null params (or known object & new event args): 您可以使用空参数(或已知对象和新事件参数)手动调用事件处理程序:

NormalCheckBoxControl_Checked(null, null);

or 要么

NormalCheckBoxControl_Checked(new object, new EventArgs());

This should manually kick off your routine, and unless you need them, then there's really no problem with providing dummy params. 这应该手动启动您的例程,除非您需要它们,否则提供虚拟参数确实没有问题。 No need to raise an event and wait for it to bubble, just call the routine. 无需引发事件并等待它冒泡,只需调用例程即可。

Of course, if there are other routines which rely on the event bubbling or if you have multiple handlers for the same event, then it might cause you a problem. 当然,如果还有其他依赖于事件冒泡的例程,或者同一事件有多个处理程序,则可能会引起问题。 Be aware of that, just in case. 请注意,以防万一。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM