title: UWP: Why behaviors in DataTemplate has a null AssociatedObject?
body: <p>I want to bind the CommandBar on NavigationView Header to a collection to dynamically display the AppBarButtons and I used the method given in <a href="https://stackoverflow.com/questions/49423784/uwp-commandbar-binding">this answer</a>. However, when I set a behavior on a CommandBar in DataTemplate and run the program, it throw an exception showing that AssociatedObject is null. Here is the code:<br />
  <strong>xaml</strong>:</p>
  <pre><code>&lt;i:Interaction.Behaviors&gt;
      &lt;behaviors:NavigationViewHeaderBehavior
          DefaultHeader=&quot;{x:Bind ViewModel.Selected.Content, Mode=OneWay}&quot;&gt;
          &lt;behaviors:NavigationViewHeaderBehavior.DefaultHeaderTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Grid&gt;
                      &lt;CommandBar Name=&quot;headerCommandBar&quot;&gt;
                          &lt;i:Interaction.Behaviors&gt;
                              &lt;behaviors:BindableCommandBarBehavior
                                  PrimaryCommands=&quot;{Binding Path=Content.ViewModel.AppBarButtonList,ElementName=shellFrame, Mode=OneWay}&quot; /&gt;
                          &lt;/i:Interaction.Behaviors&gt;
                      &lt;/CommandBar&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/behaviors:NavigationViewHeaderBehavior.DefaultHeaderTemplate&gt;
      &lt;/behaviors:NavigationViewHeaderBehavior&gt;
  &lt;/i:Interaction.Behaviors&gt;
  </code></pre>
  <p><strong>C# code in the ViewModel:</strong></p>
  <pre><code>public ObservableCollection&lt;AppBarButton&gt; AppBarButtonList { get; set; }
      = new ObservableCollection&lt;AppBarButton&gt;
      {
          new AppBarButton { Icon = new SymbolIcon(Symbol.Accept), Label=&quot;Accept&quot; },
          new AppBarButton { Icon = new SymbolIcon(Symbol.Add), Label=&quot;Add&quot; }
      };
  </code></pre>
  <p><strong>The code of behavior:</strong></p>
  <pre><code>public class BindableCommandBarBehavior : Behavior&lt;CommandBar&gt;
  {
      public ObservableCollection&lt;AppBarButton&gt; PrimaryCommands
      {
          get { return (ObservableCollection&lt;AppBarButton&gt;)GetValue(PrimaryCommandsProperty); }
          set { SetValue(PrimaryCommandsProperty, value); }
      }
  
      public static readonly DependencyProperty PrimaryCommandsProperty
          = DependencyProperty.Register(
              &quot;PrimaryCommands&quot;,
              typeof(ObservableCollection&lt;AppBarButton&gt;),
              typeof(BindableCommandBarBehavior),
              new PropertyMetadata(default(ObservableCollection&lt;AppBarButton&gt;), UpdateCommands));
  
      private static void UpdateCommands(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
      {
          if (!(dependencyObject is BindableCommandBarBehavior behavior)) return;
  
          var oldList = dependencyPropertyChangedEventArgs.OldValue as ObservableCollection&lt;AppBarButton&gt;;
          if (dependencyPropertyChangedEventArgs.OldValue != null)
          {
              oldList.CollectionChanged -= behavior.PrimaryCommandsCollectionChanged;
          }
  
          var newList = dependencyPropertyChangedEventArgs.NewValue as ObservableCollection&lt;AppBarButton&gt;;
          if (dependencyPropertyChangedEventArgs.NewValue != null)
          {
              newList.CollectionChanged += behavior.PrimaryCommandsCollectionChanged;
          }
          behavior.UpdatePrimaryCommands();
      }
  
  
      private void PrimaryCommandsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
      {
          UpdatePrimaryCommands();
      }
  
      /// &lt;summary&gt;
      /// Update the primary commands for CommandBar.
      /// &lt;/summary&gt;
      private void UpdatePrimaryCommands()
      {
          if (PrimaryCommands != null)
          {
              AssociatedObject.PrimaryCommands.Clear();
              foreach (var command in PrimaryCommands)
              {
                  AssociatedObject.PrimaryCommands.Add(command);
              }
          }
      }
  
      protected override void OnDetaching()
      {
          base.OnDetaching();
          if (PrimaryCommands != null)
          {
              PrimaryCommands.CollectionChanged -= PrimaryCommandsCollectionChanged;
          }
      }
  }
  </code></pre>
  <p>The exception is thrown in UpdatePrimaryCommands method. And I'm wondering why it happens.</p>