title: Understanding WPF data binding and value converter interactions
body: <p>I'm trying to understand what's actually happening behind the scenes on the simplified repro code below. </p>
  
  <p>I have a single <code>Window</code> with a <code>ListBox</code> and a <code>TextBlock</code> that are bound together (i.e., master -> detail). I then have a ViewModel with a couple properties--a string and a date. For the date, I implemented a value converter (<code>LongDateConverter</code>). </p>
  
  <p>I have a couple <code>Debug.WriteLine()</code> calls in the code that lead to the following output:</p>
  
  <ul>
  <li>Launch app
  <ul>
  <li><code>In converter: ConverterProblem.MainWindowViewModel</code></li>
  <li><code>In converter: null</code></li>
  </ul></li>
  <li>Click on one of the two items in the list box
  <ul>
  <li><code>In converter: ConverterProblem.DataModel</code></li>
  </ul></li>
  </ul>
  
  <p>The second and third calls to the <code>IValueConverter</code> method I think I understand. The second is <code>null</code> because the <code>ListBox</code> doesn't have a selected item yet. The third is for the item that I selected. </p>
  
  <p>What I don't understand is:</p>
  
  <ol>
  <li>Why is the first call passed a value of type <code>MainWindowViewModel</code>?</li>
  <li>Why is that call even happening in the first place?</li>
  </ol>
  
  <p>Here's my code:</p>
  
  <p><strong>MainWindow.xaml:</strong></p>
  
  <pre><code>&lt;Window x:Class="ConverterProblem.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:app="clr-namespace:ConverterProblem"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;app:LongDateConverter x:Key="longDateConverter"/&gt;
      &lt;/Window.Resources&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;ListBox SelectedItem="{Binding Data}" ItemsSource="{Binding DataList}"
                   DisplayMemberPath="Name"/&gt;
          &lt;TextBlock Text="{Binding Converter={StaticResource longDateConverter}}" 
                     DataContext="{Binding Data}" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>MainWindow.xaml.cs:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Globalization;
  using System.Windows;
  using System.Windows.Data;
  
  namespace ConverterProblem 
  {
      public class LongDateConverter : IValueConverter 
      {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
          {
              if (value == null) {
                  Debug.WriteLine("In converter: null");
                  return "null";
              }
  
              Debug.WriteLine("In converter: " + value.GetType().ToString());
  
              if (value.GetType() == typeof(MainWindowViewModel))
                  return "viewmodel";
  
              return ((DataModel)value).Date.ToLongDateString();
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
              return null;
          }
      }
  
      public class DataModel
      {
          public string Name { get; set; }
          public DateTime Date { get; set; }
      }
  
      public class MainWindowViewModel : INotifyPropertyChanged
      {
          private DataModel _data;
          private List&lt;DataModel&gt; _dataList;
  
          public MainWindowViewModel()
          {
              _dataList = new List&lt;DataModel&gt; { 
                  new DataModel { Date = DateTime.Now, Name = "John" }, 
                  new DataModel { Date = DateTime.Now.AddDays(50), Name = "Sue" }
              };
          }
  
          public DataModel Data
          {
              get { return _data; }
              set
              {
                  if (_data == value) return;
  
                  _data = value;
                  RaisePropertyChanged("Data");
              }
          }
  
          public List&lt;DataModel&gt; DataList
          {
              get { return _dataList; }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void RaisePropertyChanged(string propertyName)
          {
              var handler = PropertyChanged;
              if (handler != null) {
                  handler(this, new PropertyChangedEventArgs(propertyName));
              }
          }
      }
  
      public partial class MainWindow : Window
      {
          private MainWindowViewModel _viewModel;
  
          public MainWindow()
          {
              _viewModel = new MainWindowViewModel();
              DataContext = _viewModel;
              InitializeComponent();
          }
      }
  }
  </code></pre>