title: WPF Treeview tooltip - dynamic
body: <p>This is regards to MVVM &amp; WPF -showing dynamic tooltip based on mouse location on a WPF Treeviewitems. Let say when the user hovers over a node which has got some business data based on that it should display tooltip. </p>
  
  <p>Example: let say If i hover over manager items..should say -"locked" and for others "Ready to edit"..etc. Depends on the mouse over items data..tooltip text should guide the user to do further action. I'm trying to do some VM setting of tooltip text with the help of Tooltip opening event of TreeViewitem and trying to update the tooltip text..but facing some issue.</p>
  
  <p>What are all the best and simple way to do it. Tried behavior and end up with some error. </p>
  
  <pre><code>System.Windows.Markup.XamlParseException occurred
    Message="Cannot add content of type 'x.x.Views.CustomTreeView.TreeTooltipBehavior' to an object of type 'System.Windows.Interactivity.BehaviorCollection'.  Error at object 'x.x.x.Views.CustomTreeView.TreeTooltipBehavior' in markup file 'x.x.x.Views;component/mypage.xaml' Line 72 Position 53."
    Source="PresentationFramework"
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>&lt;MyMyControls:ExtendedTreeView x:Name="MyTreeView" ItemsSource="{Binding MyDriveCollection}"
               ItemContainerStyle="{StaticResource TVStyleTemplate}"&gt;
              &lt;MyMyControls:ExtendedTreeView.ItemTemplate&gt;
                  &lt;HierarchicalDataTemplate DataType="{x:Type NavModel:TreeDataItem}" ItemsSource="{Binding MyDriveCollection}"&gt;
                      &lt;MyControls:SimpleEditableTextBlock x:Name="TabLabel" Text="{Binding Path=MenuText, Mode=TwoWay}" 
                            ToolTip="{Binding MenuText,Mode=TwoWay}"&gt;
                      &lt;/MyControls:SimpleEditableTextBlock&gt;
                  &lt;/HierarchicalDataTemplate&gt;
              &lt;/MyControls:ExtendedTreeView.ItemTemplate&gt;
              &lt;MyControls:ExtendedTreeView.ContextMenu&gt;
                  &lt;ContextMenu ItemsSource="{Binding ContextMenuItems}"&gt;
                      &lt;ContextMenu.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;MenuItem Header="{Binding MenuText}"
                                        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, 
                                        AncestorType={x:Type TreeView}},Path=SelectedItem}" Command="{Binding Command}"&gt;
                              &lt;/MenuItem&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ContextMenu.ItemTemplate&gt;
                  &lt;/ContextMenu&gt;
              &lt;/MyControls:ExtendedTreeView.ContextMenu&gt;
              &lt;i:Interaction.Behaviors&gt;
                  &lt;CustomTreeView:TreeTooltipBehavior CustomTreeView:ToolTipOpeningCommand="{Binding ToolTipOpeningCommand,Mode=TwoWay,diag:PresentationTraceSources.TraceLevel=High}" /&gt;
                  &lt;CustomTreeView:WorkspaceTreeViewContextMenuBehavior ContextMenuOpeningCommand="{Binding ContextMenuOpeningCommand}"/&gt;
              &lt;/i:Interaction.Behaviors&gt;
   &lt;/MyControls:ExtendedTreeView&gt;
  </code></pre>
  
  <hr>
  
  <p>TreeTooltipBehavior.cs</p>
  
  <pre><code> public class TreeTooltipBehavior : Behavior&lt;ExtendedTreeViewItem&gt;
      {
          protected override void OnAttached()
          {
              base.OnAttached();
              this.AssociatedObject.ToolTipOpening += new ToolTipEventHandler(AssociatedObject_ToolTipOpening);
          }
  
          void AssociatedObject_ToolTipOpening(object sender, ToolTipEventArgs e)
          {
              if (sender != null)
              {
                  TreeDataItem hit = ((TreeDataItem) (((FrameworkElement) (sender)).DataContext));
  
                  if (ToolTipOpeningCommand != null)
                  {
                      ToolTipOpeningCommand.Execute(hit);
                  }
              }
          }
  
          public static readonly DependencyProperty ToolTipOpeningCommandProperty = DependencyProperty.Register(
              "ToolTipOpeningCommand",
              typeof(ICommand),
              typeof(TreeTooltipBehavior),
              new PropertyMetadata(null));
  
          public ICommand ToolTipOpeningCommand
          {
              get { return (ICommand)GetValue(ToolTipOpeningCommandProperty); }
              set { SetValue(ToolTipOpeningCommandProperty, value); }
          }
  
      }
  </code></pre>
  
  <hr>
  
  <p>In my view model I'm expecting to handle the ToolTipOpeningCommand and declared enough to get the event via Behavior class.</p>
  
  <p>interestingly the contextmenu behavior works fine but tooltip behavior throws xaml parser exception..</p>
  
  <p>1) Am i defined at the right place (for behavior) ?
  2) If Contextmenu behavior works then why not tooltipbehavior ?
  3) Any clue from the exception pasted at the top ?</p>
  
  <p>I'm expecting to,
   (Xaml)-tooltip behavior -> to invoke tooltipopening event in the (behavior class) -> which in turns invoke the command defined in the ViewModel. I tried this similar way for context menu and works fine. </p>
  
  <p>Pls provide some hint about fixing this issue.</p>