5

I'm creating a Templated Control in my Universal Windows Application.

The problem is that in the <Button Content="{Binding}" Command="{TemplateBinding AddCharCommand}" /> the TemplateBinding doesn't work.

It seems that the problem is because it's defined inside a DataTemplate.

This is the Style and the template applied to my control.

<Style TargetType="local:CoordinatesControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CoordinatesControl">

                <ListView ItemsSource="{TemplateBinding Numbers}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Button Content="{Binding}"
                                    Command="{TemplateBinding AddCharCommand}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • 1
    It seems we can not use the `TemplateBinding` in the `DataTemplate`. We should be able to add the command in the `Number` class. – Jayden Mar 15 '17 at 01:40

1 Answers1

0

You can't use TemplateBinding inside DataTemplate, but there are workarounds:

  1. You can create kind of proxy to some hidden element with DataContext you need. More details here.

  2. You can create kind of RelativeSource binding to find Ancestor like in WPF. More details here

Piwnik
  • 181
  • 1
  • 5