﻿using System;
using System.Windows.Controls;
using GitHub.InlineReviews.Views;
using GitHub.InlineReviews.ViewModels;

namespace GitHub.InlineReviews.Tags
{
    public partial class ShowInlineCommentGlyph : UserControl
    {
        readonly CommentTooltipView commentTooltipView;

        public ShowInlineCommentGlyph()
        {
            InitializeComponent();

            commentTooltipView = new CommentTooltipView();
            ToolTip = commentTooltipView;
            ToolTipOpening += ShowInlineCommentGlyph_ToolTipOpening;
        }

        private void ShowInlineCommentGlyph_ToolTipOpening(object sender, ToolTipEventArgs e)
        {
            var tag = Tag as ShowInlineCommentTag;

            var viewModel = new CommentTooltipViewModel();
            foreach (var comment in tag.Thread.Comments)
            {
                var commentViewModel = new TooltipCommentViewModel(comment.User, comment.Body, comment.CreatedAt);
                viewModel.Comments.Add(commentViewModel);
            }

            commentTooltipView.DataContext = viewModel;
        }
    }
}

﻿using System;
using System.Windows.Controls;
using GitHub.InlineReviews.Views;
using GitHub.InlineReviews.ViewModels;

namespace GitHub.InlineReviews.Tags
{
    public partial class ShowInlineCommentGlyph : UserControl
    {
        readonly ToolTip toolTip;

        public ShowInlineCommentGlyph()
        {
            InitializeComponent();

            toolTip = new ToolTip();
            ToolTip = toolTip;
        }

        protected override void OnToolTipOpening(ToolTipEventArgs e)
        {
            var tag = Tag as ShowInlineCommentTag;

            var viewModel = new CommentTooltipViewModel();
            foreach (var comment in tag.Thread.Comments)
            {
                var commentViewModel = new TooltipCommentViewModel(comment.User, comment.Body, comment.CreatedAt);
                viewModel.Comments.Add(commentViewModel);
            }

            var view = new CommentTooltipView();
            view.DataContext = viewModel;

            toolTip.Content = view;
        }

        protected override void OnToolTipClosing(ToolTipEventArgs e)
        {
            toolTip.Content = null;
        }
    }
}

Create and release tooltip content on demand
Create and release tooltip content on demand

Save memory by not holding on to contents of tooltip (comment thread).
