title: Click-to-Edit Control LostFocus event issue
body: <p>I am working on a simple Custom Control that should go to Edit mode by double clicking on it</p>
  
  <p>The concept is based on this question <a href="https://stackoverflow.com/questions/831796/click-to-edit-in-silverlight">Click-to-edit in Silverlight</a></p>
  
  <p>On a double click it changes initial template on Edit Template 
  and it seems to be pretty clear, except the part (5) How to change the template Back when the Control Looses the focus
  The Lost Focus event is fired only when contained controls are loosing focus 
  Here is an article that talk about it
  <a href="http://programmerpayback.com/2008/11/20/gotfocus-and-lostfocus-events-on-containers/" rel="nofollow noreferrer">http://programmerpayback.com/2008/11/20/gotfocus-and-lostfocus-events-on-containers/</a></p>
  
  <p>I have tried to implement same Technic but still no result, I cannot get LostFocus event working for me when I click outside of a control</p>
  
  <p>Where is my issue?</p>
  
  <p>My XAML</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;ContentControl x:Class="Splan_RiaBusinessApplication.Controls.TimeCodeControl"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:obj="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
      xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
      xmlns:behaviour="clr-namespace:Splan_RiaBusinessApplication.Behavior"
      xmlns:controls="clr-namespace:Splan_RiaBusinessApplication.Controls"
      xmlns:Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
      mc:Ignorable="d"
      IsTabStop="True"
      IsEnabled="True"
      Visibility="Visible"
      d:DesignHeight="100" d:DesignWidth="200"
      d:Height="200" d:Width="200"
                  &gt;
      &lt;ContentControl.Resources&gt;
          &lt;ControlTemplate x:Key="DisplayTemplate"&gt;
              &lt;Grid&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition /&gt;
                      &lt;RowDefinition /&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Target.Code, Mode=TwoWay}" /&gt;
                  &lt;StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" &gt;
                      &lt;TextBlock Text="{Binding Target.Start, Mode=TwoWay, StringFormat=hh\\:mm }" /&gt;
                      &lt;TextBlock Text='-' /&gt;
                      &lt;TextBlock Text="{Binding Target.End, Mode=TwoWay, StringFormat=hh\\:mm }" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/Grid&gt;
          &lt;/ControlTemplate&gt;
          
          &lt;ControlTemplate x:Key="EditTemplate"&gt;
              &lt;Grid Background="Aqua" Height="200" Width="200"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition /&gt;
                      &lt;RowDefinition /&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;ComboBox Width="100" Height="25" x:Name="cbTimeCode"
                          ItemsSource="{Binding TimeCodes}"
                          SelectedValue="{Binding Target.CodeId, Mode=TwoWay}"                                                                                           
                          SelectedValuePath="TimeCodeId"
                      &gt;
                      &lt;ComboBox.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;Grid&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition Width="40"/&gt;
                                      &lt;ColumnDefinition Width="100"/&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
                                  &lt;TextBlock Text="{Binding Target.Code}" /&gt;
                                  &lt;TextBlock Grid.Column="1" Text="{Binding Target.Description}" /&gt;
                              &lt;/Grid&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ComboBox.ItemTemplate&gt;
                      &lt;i:Interaction.Triggers&gt;
                          &lt;i:EventTrigger&gt;
                              &lt;behaviour:ResolveElementName PropertyName="ItemsSource" /&gt;
                          &lt;/i:EventTrigger&gt;
                      &lt;/i:Interaction.Triggers&gt;
                  &lt;/ComboBox&gt;
  
                  &lt;!--&lt;controls:TimeRangePickerControl Grid.Row="1" StartTime="{Binding Target.Start, Mode=TwoWay}"  EndTime="{Binding Target.End, Mode=TwoWay}"/&gt;--&gt;
              &lt;/Grid&gt;
          &lt;/ControlTemplate&gt;
      &lt;/ContentControl.Resources&gt;
      
      &lt;Grid x:Name="Layout" Background="Aquamarine"&gt;
          &lt;ItemsControl x:Name="PlaceHolder" Template="{StaticResource DisplayTemplate}"&gt;
          &lt;/ItemsControl&gt;
      &lt;/Grid&gt;
  &lt;/ContentControl&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Code Behind</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Linq;
  using System.Net;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Controls.Primitives;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Animation;
  using System.Windows.Shapes;
  
  
  namespace Splan_RiaBusinessApplication.Controls
  {
  public class TimeCode
  {
  
      public int TimeCodeId {get;set;}
  
      public string Code { get; set; }
  
      public string Description { get; set; }
  }
  
  public class TimeDetail
  {
      public int TimeDetailId { get;set; }
  
      public int CodeId { get;set;}
  
      public TimeSpan Start { get; set; }
      public TimeSpan End { get; set; }
  
      public string Code { get; set; }
  
      public string Comment { get; set; }
  }
  
      public partial class TimeCodeControl : ContentControl
      {
  
          public class TimeCodeControlEventArgs : EventArgs
          {
              public string userName { get; set; }
          }
  
          private static TimeSpan DoubleClickThreshold = TimeSpan.FromMilliseconds(300);
          private DateTime _lastClick;
  
          private Boolean m_EditMode = false;
          public Boolean EditMode
          {
              get { return m_EditMode; }
              set
              {
                  if (m_EditMode != value)
                  {
                      switch (value)
                      {
                          case false:
                              PlaceHolder.Template = this.Resources["DisplayTemplate"] as ControlTemplate;                            
                              break;
  
                          case true:
                              PlaceHolder.Template = this.Resources["EditTemplate"] as ControlTemplate;
                              break;
                      }
  
                      m_EditMode = value;
                  }
              }
          }
  
          public bool IsFocused
          {
              get
              {
                  return FocusManager.GetFocusedElement() == this;
              }
          }
  
  
          public TimeCodeControl()
          {
  
              TimeCodes = new List&lt;TimeCode&gt;() { new TimeCode { TimeCodeId = 200, Code= "C", Description="Cash" } };
              InitializeComponent();
  
              Layout.DataContext = this;
              this.IsTabStop = true;
              this.Visibility = Visibility.Visible;
              this.IsEnabled = true;
              this.Focus();
  
  
              Layout.MouseLeftButtonDown += Layout_MouseLeftButtonDown;
              //Layout.KeyDown += Layout_KeyDown;
              //Layout.KeyUp += Layout_KeyUp;
              this.LostFocus += TimeCodeControl_LostFocus;
              this.GotFocus += TimeCodeControl_GotFocus;
  
          }
  
  
          void TimeCodeControl_GotFocus(object sender, RoutedEventArgs e)
          {
          }
  
          void TimeCodeControl_LostFocus(object sender, RoutedEventArgs e)
          {
          }
  
          public TimeDetail Source
          {
              get { return (TimeDetail)GetValue(SourceProperty); }
              set { SetValue(SourceProperty, value); }
          }
  
          public static readonly DependencyProperty SourceProperty =
              DependencyProperty.Register("Source", typeof(TimeDetail), typeof(TimeCodeControl),
                                          new PropertyMetadata(SourceChanged));
  
          private static void SourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
          {
              var control = sender as TimeCodeControl;
              if (control == null) return;
  
              control.Target = control.Source; //.Copy();
  
          }
  
          public List&lt;TimeCode&gt; TimeCodes { get; set; }
  
          public TimeDetail Target { get; set; }
  
  
          private bool FocusIsInside(object parent)
          {
              bool rs = false;
              dynamic oFocus = FocusManager.GetFocusedElement();
              while (oFocus != null)
                  try
                  {
                      if ((oFocus.GetType() == parent.GetType()) &amp;&amp; (oFocus.Equals(this)))
                      {
                          rs = true;
                          break;
                      }
                      else
                      {
                          oFocus = oFocus.Parent;
                      }
                  }
                  catch
                  {
                      break;
                  }
  
              return rs;
          }
  
          private Boolean hasFocus = false;
  
          protected override void OnGotFocus(RoutedEventArgs e)
          {
              base.OnGotFocus(e);
              if (!hasFocus)
              {
                  hasFocus = true;
                  Debug.WriteLine("Container Got Focus");
              }
          }
          protected override void OnLostFocus(RoutedEventArgs e)
          {
              base.OnLostFocus(e);
  
              //if (!FocusIsInside(Layout))
              //{
              //    hasFocus = false;
              //    Debug.WriteLine("Container Lost Focus");
              //    EditMode = false;
              //}
  
          }
  
  
          void Layout_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
              if (DateTime.Now - this._lastClick &lt;= DoubleClickThreshold)
              {
                  EditMode = true;
                  this._lastClick = DateTime.Now;
  
                  e.Handled = true;
                  return;
              }
              this._lastClick = DateTime.Now;
  
          }
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>UPDATE :
  I decided to utilize timers to identify a scenario when user brings a focus from outside of the container or just switches focus from one control to another inside of the container. it may be not the best solution but it seems to be working for now. I would appreciate any suggestions or recommendations on different approaches or implementations.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public partial class    MyControl: ContentControl
  {
     ...
  
     public event EventHandler&lt;RoutedEventArgs&gt; LostFocus;
     public event EventHandler&lt;RoutedEventArgs&gt; GotFocus;
  
     bool Focused = false;
     DispatcherTimer FocusTimer = null;
  
      protected override void OnGotFocus(RoutedEventArgs e)
      {
          base.OnGotFocus(e);
          if (Focused) return;
  
          Focused = true;
  
          // it focused from the outside of the control
          // becouse the timer wasn't initialised on the previous LostFocused event
          // generated by other control in the same ContentControl contaner
          if (FocusTimer == null)
          {
             if (GotFocus != null)
                  GotFocus(e.OriginalSource, e);
  
              Debug.WriteLine("Got Focus ");
  
              return;
          }
  
          // It was switched from one hosted control to another one
          FocusTimer.Stop();
  
          FocusTimer = null;
      }
  
          protected override void OnLostFocus(RoutedEventArgs e)
          {
              base.OnLostFocus(e);
  
          if (e.OriginalSource is ComboBox &amp;&amp; FocusManager.GetFocusedElement() is ComboBoxItem)
              return;
  
              FocusTimer = new DispatcherTimer();
  
              Focused = false;
  
              FocusTimer.Interval = new TimeSpan(0, 0, 0, 0, 50);
              FocusTimer.Tick += (s, args) =&gt;
              {
                  FocusTimer.Stop();
                  FocusTimer = null;
  
                  // if after the timeout the focus still not triggered 
                  // by another contained element
                  // the We lost a focus on the container
                  if (!Focused )
                  {
                      if(LostFocus != null)
                          LostFocus(e.OriginalSource, e);
  
                      Debug.WriteLine("Lost Focus " );
                  }
  
              };
  
              FocusTimer.Start();
          }
  
     ...
  }</code></pre>
  </div>
  </div>
  </p>