How to mark a C # event handler as "processed"?

Let's say I have a button in the form that I want to turn off if some condition is met. Is there a way to check this condition inside the "IsEnabled" event handler and change the allowed state so that the activated state does not cause another call to the IsEnabled event handler a second time?

Let me demonstrate:

private void ExportResults_IsEnabledChanged (object sender, DependencyPropertyChangedEventArgs e)
{
 if (some condition)
 {
  uxExportResults.IsEnabled = false; // this will cause another call to the event handler, eventually resulting in a stack overflow
 }
}

Suppose I fire an event elsewhere (that I am).

+3
source share
3 answers
if (someCondition && uxExportResults.IsEnabled) { ... }

This will disable your control if it is enabled.

+4
source

Another option is to temporarily disable the event as follows:

private void ExportResults_IsEnabledChanged (object sender, DependencyPropertyChangedEventArgs e)
{
    if (some condition)
    {
        uxExportResults.IsEnabledChanged -= ExportResults_IsEnabledChanged;
        try
        {
            uxExportResults.IsEnabled = false; // this will cause another call to the event handler, eventually resulting in a stack overflow
        }
        finally
        {
            uxExportResults.IsEnabledChanged += ExportResults_IsEnabledChanged;
        }
    }
}
+3

- IsEnabled .

private void ExportResults_IsEnabledChanged (object sender, DependencyPropertyChangedEventArgs e)
{
  if (uxExportResults.IsEnabled == true)
  {
    uxExportResults.IsEnabled = false;
  }
}

, , IsEnabled , .

public bool IsEnabled
{
  get { return isEnabled; }
  set
  {
    if(isEnabled != value)
    {
      isEnabled = value;
      IsEnabledChanged(this,args);
    }
  }
}
+2
source

Source: https://habr.com/ru/post/1717657/


All Articles