XAML TemplateBinding within a DataTemplate
<p>I'm creating a templated control for a UWP app, and I've hit a snag when trying to bind within a nested DataTemplate. Here's my control XAML in Themes/Generic.xaml:</p>

<pre><code>&lt;Style TargetType="local:EnhancedListView"&gt;
    &lt;Setter Property="Template"&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate TargetType="local:EnhancedListView"&gt;
                &lt;Grid HorizontalAlignment="Stretch"&gt;
                    &lt;Grid.Resources&gt;
                        &lt;DataTemplate x:Key="ListViewTemplate"&gt;
                            &lt;Grid&gt;
                                &lt;Grid.ColumnDefinitions&gt;
                                    &lt;ColumnDefinition /&gt;
                                    &lt;ColumnDefinition /&gt;
                                &lt;/Grid.ColumnDefinitions&gt;
                                &lt;CheckBox Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}, Path=IsCheckModeEnabled}" Grid.Column="0" /&gt;
                                &lt;TextBlock Text="{Binding}" Grid.Column="1" /&gt;
                            &lt;/Grid&gt;
                        &lt;/DataTemplate&gt;
                    &lt;/Grid.Resources&gt;
                    &lt;Grid.RowDefinitions&gt;
                        &lt;RowDefinition /&gt;
                        &lt;RowDefinition /&gt;
                    &lt;/Grid.RowDefinitions&gt;
                    &lt;CheckBox Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}, Path=IsCheckModeEnabled}" Grid.Row="0"&gt;Hello&lt;/CheckBox&gt;
                    &lt;ListView Grid.Row="1" ItemsSource="{TemplateBinding ItemsSource}" ItemTemplate="{StaticResource ListViewTemplate}" /&gt;
                &lt;/Grid&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;
</code></pre>

<p>Here's my actual control/converter:</p>

<pre><code>public class EnhancedListView : Control
{
    public EnhancedListView()
    {
        DefaultStyleKey = typeof(EnhancedListView);
    }

    public object ItemsSource
    {
        get { return GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(EnhancedListView), new PropertyMetadata(null));

    public bool IsCheckModeEnabled
    {
        get { return (bool)GetValue(IsCheckModeEnabledProperty); }
        set { SetValue(IsCheckModeEnabledProperty, value); }
    }

    public static readonly DependencyProperty IsCheckModeEnabledProperty = DependencyProperty.Register("IsCheckModeEnabled", typeof(bool), typeof(EnhancedListView), new PropertyMetadata(null));
}

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (bool)value ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotSupportedException();
    }
}
</code></pre>

<p>Here's the MainPage.xaml:</p>

<pre><code>&lt;local:EnhancedListView IsCheckModeEnabled="False" x:Name="ctlListView"&gt;
&lt;/local:EnhancedListView&gt;
</code></pre>

<p>And finally, my MainPage.xaml.cs:</p>

<pre><code>    public MainPage()
    {
        this.InitializeComponent();

        ctlListView.ItemsSource = new List&lt;string&gt; { "Item 1", "Item 2" };
    }
</code></pre>

<p>As I would expect, when the page loads, the first checkbox is hidden because IsCheckModeEnabled is false, but all the checkboxes nested within the DataTemplate are still visible.</p>

<p>I've tried wrapping this into a StaticResource as suggested <a href="https://stackoverflow.com/questions/18265278/reference-viewmodels-datacontext-from-within-a-datatemplate-in-a-datatemplate">here</a>, but it doesn't work with some complex types, such as nesting another DataTemplate within my DataTemplate.</p>

<p>I'm certain the binding isn't quite right on the Visibility property here:</p>

<p><code>&lt;CheckBox Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}, Path=IsCheckModeEnabled}" Grid.Column="0" /&gt;</code></p>

<p>Thanks for your help!</p>