为什么我不能在数据模板中使用{x:Bind {RelativeSource Self}}?

ven*_*eis 5 c# winrt-xaml uwp

如果我{x:Bind {RelativeSource Self}}在数据模板中使用,编译时会出现以下错误:

你调用的对象是空的.

我们的想法是将模板化对象传递给属性,如命令参数.以下是MainPage.xaml的示例:

<Page
    x:Class="XBindTest5.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XBindTest5"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <ResourceDictionary>
            <local:OpenItemCommand x:Key="OpenCommand"/>
        </ResourceDictionary>
    </Page.Resources>

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ItemsControl ItemsSource="{x:Bind NewsItems, Mode=OneWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate x:DataType="local:NewsItem">
                    <StackPanel>
                        <Button Command="{x:Bind {StaticResource OpenCommand}}" CommandParameter="{x:Bind {RelativeSource Self}}">
                            <TextBlock Text="{x:Bind Title}"/>
                        </Button>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)

代码-behinde文件MainPage.xaml.cs中定义了一个简单的模型:

using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Windows.UI.Xaml.Controls;


namespace XBindTest5 {

    public class NewsItem {
        public string Title { get; set; }
    }

    /// <summary>
    ///     command to open the item
    /// </summary>
    public class OpenItemCommand : ICommand {

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter) {
            return true;
        }

        public void Execute(object parameter) {
            // ... example ...
        }
    }

    public sealed partial class MainPage : Page {

        public ObservableCollection<NewsItem> NewsItems { get; set; }
            = new ObservableCollection<NewsItem>(new[] {
                new NewsItem() { Title = "Item 1" },
                new NewsItem() { Title = "Item 2" } });

        public MainPage() {
            this.InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jay*_*Zuo 11

虽然看起来你已经解决了你的问题,但我仍然想做一些澄清以避免混淆,并为未来的读者清楚地说明.

正如@Peter Duniho所提到的,{x:Bind}无法使用DataContext属性并且{x:Bind}没有Source属性,因此您不能将其StaticResource用作数据上下文{x:Bind},但您可以使用属性或静态路径.使用时{x:Bind},它使用背景类作为其数据上下文.例如,在设置时ItemsSource="{x:Bind NewsItems, Mode=OneWay}",它使用XBindTest5.MainPage类作为其数据上下文并将NewsItems此类的属性绑定到ItemsSource.而在DataTemplate中,{x:Bind}使用声明的类x:DataType作为其数据上下文.请注意DataTemplate和x:DataType中的以下说明:

DataTemplate内部(无论是用作项目模板,内容模板还是标题模板),Path的值不会在页面的上下文中解释,而是在模板化的数据对象的上下文中解释.因此,在编译时可以验证其绑定(以及为它们生成有效的代码),DataTemplate需要使用x:DataType声明其数据对象的类型.

在您的情况下,您使用Commandin DataTemplate,因此您可以添加OpenCommand属性NewsItem并绑定此属性Command以使用它.

在您的代码隐藏中:

public class NewsItem
{
    public string Title { get; set; }
    public OpenItemCommand OpenCommand { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在XAML中:

<DataTemplate x:DataType="local:NewsItem">
    <StackPanel>
        <Button Command="{x:Bind OpenCommand}" CommandParameter="{x:Bind}">
            <TextBlock Text="{x:Bind Title}" />
        </Button>
    </StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

另外{x:Bind}不支持{RelativeSource},通常你可以命名的元素,并使用其名称Path作为替代.有关更多信息,请参阅{x:Bind}和{Binding}功能比较.

但这不能用,DataTemplate因为所有人Path都应该是属性NewsItem.在你的情况下,我认为你想要传递的NewsItem不是Button,所以你可以CommandParameter="{x:Bind}"用来传递NewsItemas CommandParameter.

PS:XAML设计器中存在一个小错误,您可能仍会收到Object reference not set to an instance of an object.错误消息.你可以在Bind之后添加一个空格,就像{x:Bind }一个变通方法.