title: Using a IConverter to deal with {NewItemPlaceholder} in WPF / XAML / MVVM
body: <p>Here is my DataTemplate:</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;converter:PlaceholderConverter x:Key="_placeholderConverter"/&gt;
  
      &lt;!-- Data(Display)Template for data objects of x:Type Customer--&gt;
      &lt;DataTemplate DataType="{x:Type model:Customer}"&gt;
          &lt;!-- Customer Properties will be vertically stacked --&gt;
          &lt;ContentControl &gt;
              &lt;StackPanel&gt;
                  &lt;TextBlock Text="{Binding FirstName}"/&gt;
                  &lt;TextBlock Text="{Binding LastName}"/&gt;
                  &lt;TextBlock Text="{Binding Phone}"/&gt;
              &lt;/StackPanel&gt;
          &lt;/ContentControl&gt;
      &lt;/DataTemplate&gt;
  &lt;UserControl.Resources&gt;
  </code></pre>
  
  <p>And the two different 'container's:</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="25"/&gt;
          &lt;RowDefinition Height="100"/&gt;
          &lt;RowDefinition Height="*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;Button Grid.Row="0"
              Content="Delete"
              HorizontalAlignment="Left"
              VerticalAlignment="Top"
              Width="75"
              Command="{Binding DeleteCommand}"/&gt;
  
      &lt;DataGrid Grid.Row="1" 
                ItemsSource="{Binding Customers}" 
                SelectedItem="{Binding SelectedCustomer}" 
                AutoGenerateColumns="True"/&gt;
  
      &lt;ListBox 
          Grid.Row="2" 
          ItemsSource="{Binding Customers, Mode=OneWay}"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>And the app:</p>
  
  <p><a href="https://i.stack.imgur.com/e2R8q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e2R8q.png" alt="enter image description here"></a></p>
  
  <ol>
  <li>How to remove the {NewItemPlaceholder}? [Done, solution below].</li>
  <li>How to prevent the binding error that mention "{NewItemPlaceholder}" when clicking in one of the empty rows in the table above intending on adding a new row (I can still add rows).</li>
  </ol>
  
  <p>The errors:</p>
  
  <pre><code>...Cannot convert '{NewItemPlaceholder}' from type 'NamedObject' to type 'CustomerExample.Model.Customer'...
  
  
  ...ConvertBack cannot convert value '{NewItemPlaceholder}' (type 'NamedObject'). BindingExpression:Path=SelectedCustomer; DataItem='CustomerViewModel'...
  </code></pre>
  
  <p>I can write an IConverter implementation, but how to tie it in to the XAML? </p>
  
  <p>thanks in advance :-)</p>
  
  <p>Here is the implementation of the IConverter:</p>
  
  <pre><code>public class PlaceholderConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          if (value != null &amp;&amp; value.ToString() == "{NewItemPlaceholder}")
              return DependencyProperty.UnsetValue;
          return value;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>and to bind to individual items, the XAML goes something like:</p>
  
  <pre><code>&lt;TextBlock Text="{Binding Name, Converter={StaticResource PlaceholderConverter}}"/&gt;
  </code></pre>
  
  <p>But I think I need to add it 'globally' to the data collection elements, not to where individual properties are being bound.</p>