title: Switching a Control over different windows inside ContentControl
body: <p>I am making a video player. In is application I have a full screen window and a normal window.</p>
  
  <p>I am using a <code>ContentControl</code> to bind a <code>MediaElement</code> inside it, from my ViewModel. The instance of my <code>MediaElement</code> is created inside the ViewModel so it easier to control this control via code. </p>
  
  <p>Now I have a <code>Button</code> to show another window with another <code>ContentControl</code> where I can assigned this very same <code>MediaElement</code>. It works. The element switch from my normal window to my FullScreen.</p>
  
  <p>Problem is when I close my full screen window it does not switch back, to my normal window <code>ContentControl</code>.</p>
  
  <p>I tried rebinding the control, it works the control is seen inside the control control but does not appear to work, to any of the commands.</p>
  
  <p>FullScreenView</p>
  
  <pre><code>&lt;Grid&gt;
     &lt;ContentControl Content="{Binding PlayerBaseViewModel.MediaControl, Mode=OneWay}" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>PlayerBaseView</p>
  
  <pre><code>&lt;Grid&gt;
     &lt;ContentControl Content="{Binding MediaControl}" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Call to Fullscreen </p>
  
  <pre><code>internal void ShowFullScreen(PlayerBaseViewModel playerBaseViewModel)
  {
      var fullScreenView = new FullScreenView();
  
      var fullScreenViewModel = new FullScreenViewModel(
          playerBaseViewModel, 
          this.PlayControlViewModel, 
          this.source);
  
      fullScreenView.DataContext = fullScreenViewModel;
      fullScreenView.ShowDialog();
      fullScreenViewModel.Dispose();
  }
  </code></pre>
  
  <p>Base </p>
  
  <pre><code>public FrameworkElement MediaControl
  {
      get
      {
          return this.mediaControl;
      }
  
      set
      {
          this.mediaControl = value;
          if (this.mediaControl != null)
          {
              this.mediaControl.HorizontalAlignment = HorizontalAlignment.Center;
          }
  
          this.RaisePropertyChanged(() =&gt; this.MediaControl);
      }
  }
  private void FullScreen()
  {
      if (this.ShowFullScreen != null)
      {
          MessageHelper.ShowMessage(
              string.Format(StaticData.MessagePlayerBaseViewModelFullScreen, this.Device.Name),
              string.Format(StaticData.MessagePlayerBaseViewModelFullScreenDetails, this.Device.Name),
              3);
          var control = this.mediaControl;
          this.ShowFullScreen(this);
          this.MediaControl = null;
          control.DataContext = this;
          this.MediaControl = control;
          this.Device.ResetAllDevice();
      }
  }
  </code></pre>
  
  <p>PlayerAudioVideoViewModel </p>
  
  <pre><code>public class PlayerAudioVideoViewModel : PlayerBaseViewModel
  {
      #region Static Fields
  
      private static readonly Stopwatch Watch = Stopwatch.StartNew();
  
      #endregion
  
      #region Fields
  
      private readonly DependencyPropertyDescriptor mediaPositionDependencyPropertyDescriptor =
          DependencyPropertyDescriptor.FromProperty(MediaSeekingElement.MediaPositionProperty, typeof(UIElement));
  
      private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Render);
  
      private MediaElement mediaElement = new MediaElement();
  
      #endregion
  
      #region Constructors and Destructors
  
      public PlayerAudioVideoViewModel(DeviceModel deviceModel)
          : base(deviceModel)
      {
          this.MediaControl = this.mediaElement;
          this.mediaElement.BeginInit();
          this.timer.Interval = TimeSpan.FromMilliseconds(200);
          this.timer.Tick += this.TimerTick;
          this.mediaElement.LoadedBehavior = MediaState.Manual;
          this.mediaElement.ScrubbingEnabled = true;
          this.mediaElement.MediaOpened += this.MediaOpened;
          this.mediaElement.MediaEnded += this.MediaEnded;
          this.mediaPositionDependencyPropertyDescriptor.AddValueChanged(this.mediaElement, this.PositionChanged);
          this.mediaElement.EndInit();
      }
  
      #endregion
  
      #region Public Methods and Operators
  
      public override void CleanUp()
      {
          this.MediaControl = null;
          this.mediaElement.Source = null;
          this.mediaElement.MediaOpened -= this.MediaOpened;
          this.mediaElement.MediaEnded -= this.MediaEnded;
          this.mediaElement.LoadedBehavior = MediaState.Manual;
          this.mediaElement.UnloadedBehavior = MediaState.Manual;
          this.timer.Tick -= this.TimerTick;
          this.mediaElement.Source = null;
          this.mediaElement = null;
      }
  
      public override void LoadData()
      {
          if (this.Data != null)
          {
              var cont = (ContentMoving)this.Data.Content;
              if (this.mediaElement.Source == null || !this.mediaElement.Source.LocalPath.Equals(cont.Path.FullName))
              {
                  this.mediaElement.Source = new Uri(cont.Path.FullName, UriKind.Absolute);
                  this.mediaElement.Pause();
              }
              else
              {
                  this.mediaElement.Position =
                      cont.Position.Subtract(new TimeSpan(0, 0, 0, 0, cont.Position.Milliseconds));
  
                  // this.ChangePlaySpeed();
                  this.timer.Start();
              }
          }
          else
          {
              this.mediaElement.Source = null;
          }
          Debug.Print("D {0} Load.", this.DeviceName);
  
      }
  
      public override void MuteAudio(bool mute)
      {
          this.mediaElement.IsMuted = mute;
      }
  
      public override void Pause()
      {
          Debug.Print("D {0} Pause.", this.DeviceName);
          this.mediaElement.Pause();
      }
  
      public override void Play()
      {
          Debug.Print("D {0} Play.", this.DeviceName);
          this.mediaElement.Play();
      }
  
  
  
  
  
      #endregion
  
      #region Methods
  
  
      private void MediaEnded(object sender, RoutedEventArgs e)
      {
          if (this.mediaElement.Position == this.mediaElement.NaturalDuration)
          {
              this.DataEndReached();
          }
      }
  
      private void MediaOpened(object sender, RoutedEventArgs e)
      {
          if (this.Data != null)
          {
              var cont = (ContentMoving)this.Data.Content;
              if (this.Device.Status == PlayStatus.Play)
              {
                  this.Device.ResetAllDevice();
              }
              else
              {
                  this.mediaElement.Position =
                      cont.Position.Subtract(new TimeSpan(0, 0, 0, 0, cont.Position.Milliseconds));
                  this.ChangePlaySpeed();
              }
          }
  
          this.DataLoaded();
      }
  
  
  
  
      #endregion
  }
  </code></pre>
  
  <p>}</p>