Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding with a converter inside a template
    text
    copied!<p>I have defined a DataTemplate for a ListBox. Inside the template, I use TextBlock controls to display the properties of the data context. For example:</p> <pre><code>&lt;TextBlock Text="{Binding Path=FirstName}" /&gt; </code></pre> <p>And if I do this:</p> <pre><code>&lt;TextBlock Visibility="{Binding Path=IsAccountValid}" /&gt; </code></pre> <p>...the application runs, but there is a warning in the output about trying to bind a boolean property to a Visibility enumeration.</p> <p>If I do this:</p> <pre><code>&lt;TextBlock Visibility="{Binding Path=IsAccountValid,Converter={StaticResource visibilityOfBool}}" /&gt; </code></pre> <p>and somewhere in my App.xaml is:</p> <pre><code>&lt;BooleanToVisibilityConverter x:Key="visibilityOfBool" /&gt; </code></pre> <p>I get a null reference exception.</p> <p>I suspected this might be because the property IsAccountValid is not a dependency property, so I added a CheckBox to the window, and did this:</p> <pre><code>&lt;TextBlock Visibility="{Binding Path=IsChecked,Converter={StaticResource visibilityOfBool},ElementName=butA}" /&gt; </code></pre> <p>But got the same error.</p> <p>Why? The DataContext object is valid because if I bind IsAccountValid to the Text property, the value is correctly displayed.</p> <p>The converter is never called, so I am wondering if it is the converter that cannot be found.</p> <p>Why can the converter not be found? Why can the converter be found outside the data template and not inside the data template?</p> <p>I tried building the template again with Blend, as Blend usually gets it right, but the code it generated was the same as mine.</p> <p>I tried some of the fixes suggested on this website, including setting RelativeSource to TemplateParent and Self, but it made no difference.</p> <p>What is going on?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload