title: Window Loaded and WPF
body: <p>I have a WPF project in Windows 2012 in which I need to load some information in the Window Loaded event.  I need to do this in the View Model rather than in the CodeBehind, though.  I am attempting to use the following code:</p>
  
  <p>In my xaml:</p>
  
  <pre><code>&lt;interactivity:Interaction.Behaviors&gt;
      &lt;behaviors:WindowLoadedBehavior LoadedCommand="{Binding WindowLoadedCommand}" /&gt;
  &lt;/interactivity:Interaction.Behaviors&gt;
  </code></pre>
  
  <p>In my View Model:</p>
  
  <pre><code>private DelegateCommand _WindowLoadedCommand;
  
  public DelegateCommand WindowLoadedCommand
  {
      get
      {
          return _WindowLoadedCommand;
      }
      private set
      {
          _WindowLoadedCommand = value;
      }
  }
  
  public ShellViewModel()
  {
      WindowLoadedCommand = new DelegateCommand(WindowLoadedAction);
  }
  
  protected void WindowLoadedAction()
  {
      ...
  }
  </code></pre>
  
  <p>My attached behavior:</p>
  
  <pre><code>public class WindowLoadedBehavior : Behavior&lt;FrameworkElement&gt;
  {
      [SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Dependency Property.  Allow public.")]
      public static DependencyProperty LoadedCommandProperty = DependencyProperty.Register("LoadedCommand", typeof(ICommand), typeof(WindowLoadedBehavior), new PropertyMetadata(null));
  
      public ICommand LoadedCommand
      {
          get { return (ICommand)GetValue(LoadedCommandProperty); }
          set { SetValue(LoadedCommandProperty, value); }
      }
  
      protected override void OnAttached()
      {
          base.OnAttached();
  
          AssociatedObject.Loaded += AssociatedObject_Loaded;
      }
  
      protected override void OnDetaching()
      {
          AssociatedObject.Loaded -= AssociatedObject_Loaded;
  
          base.OnDetaching();
      }
  
      private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
      {
          if (LoadedCommand != null)
              LoadedCommand.Execute(null);
      }
  }
  </code></pre>
  
  <p>The OnAttached, AssociatedObject_Loaded and LoadedCommand get are all firing, but the LoadedCommand set is not firing and, obviously, the WindowLoadedCommand isn't firing.  Any clue what I can do to get this working?</p>