3

With the {x:Bind} markup syntax you can bind to events provided the method meets the following requirements:

  • Match the signature of the event.
  • OR have no parameters.
  • OR have the same number of parameters of types that are assignable from the types of the event parameters.

This works perfectly fine outside of a DataTemplate. Once the binding happens inside the DataTemplate the compiler generates the following error:

Xaml Internal Error error WMC9999: Object reference not set to an instance of an object.

What is the fix for binding to events inside DataTemplates?

Full example code here.

Snippet of the example code below - note the first button (line 2) is fine and the second button (line 6) is also fine. If you comment out line 6 and and comment in line 7, the error occurs.

 <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Tapped="{x:Bind Click}" Content="WORKING"/>
        <ListView ItemsSource="{x:Bind Names}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Customer"> 
                    <Button Content="{x:Bind Title}"/>
                    <!--<Button Tapped="{x:Bind Clicky}" Content="{x:Bind Title}"/>-->
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
  • Just to be clear, do you get this xaml error in design time or as a runtime error? Also where does the `Clicky` event actually reside; is it found on the page's datacontext? – ΩmegaMan Sep 29 '15 at 18:00
  • Compile time - won't actually build. `Clicky` resides on the object instance, not the page's datacontext. If this was Command + ICommand I would do `{Binding Clicky}` to access it – Robert MacLean Sep 29 '15 at 18:42

2 Answers2

5

I was able to get it to work with the following code:

<DataTemplate x:DataType="local:Customer">
     <StackPanel Orientation="Vertical">
         <Button Tapped="{x:Bind Clicky}" Content="{x:Bind Title}" />
     </StackPanel>
</DataTemplate>

It seems as though you need to have it inside a container for it work. I have no idea why I am guessing magic.

davemsdevsa
  • 464
  • 4
  • 5
0

The parser cannot find Clicky from the datacontext of the button while in the template. Because the object that is being handed to the button in the template (from the Names on the ItemSource of the parent) is not the same as outside the template which has a Clicky. You will need to bind Clicky to the page's datacontext to get it to work.


Otherwise turn off any design time operations by setting Tapped="{x:Bind Clicky, IsDesignTimeCreatable=False}.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • If Clicky was on the pages DataContext, it is not possible with {x:Bind} because of this issue: http://stackoverflow.com/questions/32849864/alternative-to-elementname-in-xbind-with-datatemplates However Clicky is on the context of the item that is bound so no traversal should be needed. – Robert MacLean Sep 29 '15 at 18:43
  • @RobertMacLean is the exception due to operations in `Clicky`? See update, – ΩmegaMan Sep 29 '15 at 18:51
  • Nope - not with that at all. Happens when it compiles so not hitting design time stuff – Robert MacLean Sep 29 '15 at 19:06