title: How to pass information from an attached behavior to the viewmodel with static events?
body: <p>I have an attached property to textboxes in my view. The attached property performs validation on the textbox input and performs other chores. The attached property validation routine raises an event which is being watched by the viewmodel. </p>
  
  <ol>
  <li>Does this "violate" MVVM reasoning by having the viewmodel obtain the invalid TextBoxes?</li>
  <li>How will the GC deal with the static events from the attached property when the usercontrol containing the textboxes is removed?</li>
  <li>If specific code is needed to avoid memory leaks, how is that done?</li>
  <li>Is there a preferred way to do this?</li>
  </ol>
  
  <p>Sorry for the long list, but Google does not address this situation.</p>
  
  <p>Any and all help is appreciated. Thank you for your consideration.</p>
  
  <p>(VS2010 .net 4.5)</p>
  
  <p>TIA</p>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>class CheckInViewModel : SimpleViewModelBase
      {
          public CheckInViewModel()
          {
              InValidTextBoxes = new List&lt;TextBox&gt;();
  
              Stargate_V.Helpers.ColorMaskingTextBoxBehavior.Validated += (sender, e) =&gt;
                  {
                      if (e.valid)
                          InValidTextBoxes.Remove(e.sender);
                      else
                          InValidTextBoxes.Add(e.sender);
                  };
          }
  
          List&lt;TextBox&gt; InValidTextBoxes;
  
  
      }
  </code></pre>
  
  <p><strong>XAML</strong></p>
  
  <pre><code> &lt;TextBox 
              h:ColorMaskingTextBoxBehavior.Mask="^[MmFf]$"
              Text="{Binding Sex}"
              Height="24" HorizontalAlignment="Right" Margin="0,55,665,0" VerticalAlignment ="Top" Width="36" /&gt;
  </code></pre>
  
  <p><strong>Attached Properity</strong></p>
  
  <pre><code>  public class ColorMaskingTextBoxBehavior : DependencyObject
      {
          // Entrance point from Xaml 
          public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask",
                  typeof(string),
                  typeof(ColorMaskingTextBoxBehavior),
                  new FrameworkPropertyMetadata(OnMaskChanged));
  ...........................
  
   // Callback from XAML initialization of the attached property.
      private static void OnMaskChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
      {
          var textBox = dependencyObject as TextBox;
          var mask = e.NewValue as string;
          textBox.PreviewTextInput -= textBox_PreviewTextInput;
          textBox.PreviewKeyDown -= textBox_PreviewKeyDown;
          DataObject.RemovePastingHandler(textBox, Pasting);
          DataObject.RemoveCopyingHandler(textBox, NoDragCopy);
          CommandManager.RemovePreviewExecutedHandler(textBox, NoCutting);
  
  
          if (mask == null)
          {
              textBox.ClearValue(MaskProperty);
              textBox.ClearValue(MaskExpressionProperty);
          }
          else
          {
              textBox.SetValue(MaskProperty, mask);
              SetMaskExpression(textBox, new Regex(mask, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace));
              textBox.PreviewTextInput += textBox_PreviewTextInput;
              textBox.PreviewKeyDown += textBox_PreviewKeyDown;
              DataObject.AddPastingHandler(textBox, Pasting);
              DataObject.AddCopyingHandler(textBox, NoDragCopy);
              CommandManager.AddPreviewExecutedHandler(textBox, NoCutting);
          }
      }
  
  
  
  private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
          {
              var textBox = sender as TextBox;
              var maskExpression = GetMaskExpression(textBox);
  
              string passHex = (string)textBox.GetValue(PassColorProperty);
              string failHex = (string)textBox.GetValue(FailColorProperty);
              Color passColor = Extensions.ToColorFromHex(passHex);
              Color failColor = Extensions.ToColorFromHex(failHex);
  
              if (maskExpression == null)
              {
                  return;
              }
  
              var proposedText = GetProposedText(textBox, e.Text);
  
              if (!maskExpression.IsMatch(proposedText))
              {
                  textBox.Background = new SolidColorBrush(failColor);
  
                  ValidationEventArgs args = new ValidationEventArgs();
                  args.sender = textBox;
                  args.valid = false;
                  OnValidation(args);
              }
              else
              {
                  textBox.Background = new SolidColorBrush(passColor);
  
                  ValidationEventArgs args = new ValidationEventArgs();
                  args.sender = textBox;
                  args.valid = true;
                  OnValidation(args);
              }
          }
  </code></pre>
  
  <p><strong>Event Called from the above code</strong></p>
  
  <pre><code>    public static event EventHandler&lt;ValidationEventArgs&gt; Validated;
  
      static void OnValidation(ValidationEventArgs e)
      {
          EventHandler&lt;ValidationEventArgs&gt; handler = Validated;
          if (handler != null)
          {
              handler(null, e);
          }
      }
  
  
  public class ValidationEventArgs : EventArgs
  {
      public TextBox sender;
      public bool valid;
  }
  </code></pre>