Although it seems that you have solved your problem, I still want to make some clarifications in order to avoid confusion and make it understandable to future readers.
As @Peter Duniho noted, {x:Bind} cannot work with the DataContext property, and {x:Bind} does not have the Source property, so you cannot use StaticResource as the data context in {x:Bind} , but instead you can use a property or a static path. When using {x:Bind} it uses the background class as its data context. For example, when you set ItemsSource="{x:Bind NewsItems, Mode=OneWay}" , it uses the XBindTest5.MainPage class as its data context and binds the NewsItems property of this class to ItemsSource . And, while inside the DataTemplate, {x:Bind} uses the class declared in x:DataType as its data context. Please pay attention to the following explanation in DataTemplate and x: DataType :
Inside a DataTemplate (used as an element template, content template, or header template), the value Path is not interpreted in the context of the page, but in the context of the data template. So that its bindings can be checked (and the efficient code generated for them) at compile time, the DataTemplate must declare the type of its data object using x: DataType .
In your case, you use the Command in the DataTemplate , so you can add the OpenCommand property to the NewsItem and bind this property to Command to use it.
In your code:
public class NewsItem { public string Title { get; set; } public OpenItemCommand OpenCommand { get; set; } }
In XAML:
<DataTemplate x:DataType="local:NewsItem"> <StackPanel> <Button Command="{x:Bind OpenCommand}" CommandParameter="{x:Bind}"> <TextBlock Text="{x:Bind Title}" /> </Button> </StackPanel> </DataTemplate>
Also {x:Bind} does not support {RelativeSource} , usually you can name this element and use its name in Path as an alternative. See the {x: Bind} and {Binding} function comparison for more information.
But this cannot be used in a DataTemplate , since all Path must be a NewsItem property. And in your case, I think you want to pass the NewsItem not a Button , so you can use CommandParameter="{x:Bind}" to pass the NewsItem as a CommandParameter .
PS: There is a small mistake in the XAML design, you can still get the error Object reference not set to an instance of an object. . You can add a space after Bind as {x:Bind } as a workaround.