title: Specifying DataContext can cause Xaml to not see it not null when it is?
body: <p>I have encountered a strange problem in my Wpf application on the Xaml side and I don't have an explanation for it.</p>
  <p>Scenario: I have a datagrid binded to some complex classes, some rows have a &quot;details&quot; class with some extra data (<code>VSmtp</code> in the code), while others don't. My plan was to have a small form appear if the user selected a row with that extra data.</p>
  <p>To do so I made a custom converter that enabled visibility for a control if the binded object is not null:</p>
  <pre class="lang-cs prettyprint-override"><code>    public class NotNullValueToVisibilityConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return value != null ? Visibility.Visible : Visibility.Collapsed;
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
  </code></pre>
  <p>I binded the visibility of the form to the selected item of the datagrid (<code>SelectedItem</code> in the code) with this converter, tested it and it worked exactly as expected, the form appeared only if the user selected a row and that row had the extra class set</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;local:ucSmtpForm 
      Visibility=&quot;{Binding SelectedItem.VSmtp, Converter={StaticResource NotNullValueToVisibilityConverter}}&quot;
      &gt;
  &lt;/local:ucSmtpForm&gt;
  </code></pre>
  <p>Happy with the results, i proceeded to bind the data to the form, however when i did that and tested again, I discovered that the form now appeared on every row, even those without the <code>VSmtp</code> class</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;local:ucSmtpForm 
      Visibility=&quot;{Binding SelectedItem.VSmtp, Converter={StaticResource NotNullValueToVisibilityConverter}}&quot;
      DataContext=&quot;{Binding SelectedItem.VSmtp}&quot;
      &gt;
  &lt;/local:ucSmtpForm&gt;
  </code></pre>
  <p>I have no idea for the reason the form now is visible on rows without the <code>VSmtp</code> class while before it worked fine, debugging the code confirms that the property is null where it should be.</p>
  <p>Does anyone have an explanation for this?</p>
  <p>I have already solved the issue by simply putting the form in a grid and moved the Visibility binding to it, now works as intended, but I wanted to know why it didn't work if the DataContext and Visibility were both binded in the UserControl, to be better informed for the future.</p>
  <p>Thanks in andvance</p>