Xamarin.ListView。绑定如何在itemtemplate/datatemplate中工作?

Xamarin.ListView。绑定如何在itemtemplate/datatemplate中工作?,listview,xamarin,binding,datatemplate,itemtemplate,Listview,Xamarin,Binding,Datatemplate,Itemtemplate,我有这个XAML代码 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Android_1.MainPage" Title=""> <

我有这个XAML代码

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Android_1.MainPage" Title="">
    <StackLayout>
        <Label Text="{Binding Type}"/>
        <Label Text="{Binding Question}"/>
        <ListView ItemsSource="{Binding Answers}"/>
        <Button Text="Next Question" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>
现在它是这样工作的。 上面的代码有效。所有绑定都可以工作

当我更改XAML时,ListView中的绑定停止工作, 标签中的绑定仍在工作:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Android_1.MainPage" Title="">
    <StackLayout>
        <Label Text="{Binding Type}"/>
        <Label Text="{Binding Question}"/>
        <ListView ItemsSource="{Binding Answers}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label LineBreakMode="WordWrap"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Text="Next Question" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>


这很有效

您的数据模板只是一个空标签。我想这就是为什么它似乎不起作用。您需要绑定标签的Text属性,或硬编码一个值是的,您需要绑定标签的Text属性。此外,您可以标记此答案,这将帮助更多有相同问题的人:)。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Android_1.MainPage" Title="">
    <StackLayout>
        <Label Text="{Binding Type}"/>
        <Label Text="{Binding Question}"/>
        <ListView ItemsSource="{Binding Answers}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label LineBreakMode="WordWrap"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Text="Next Question" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>
  public class MyView : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private QuestCase questCase;

            public QuestCase QuestCase
            {
                get { return questCase; }
                set {
                    if(value != questCase)
                    {
                        questCase = value;
                        OnPropertyChanged("Question");
                        OnPropertyChanged("Type");
                        OnPropertyChanged("Answers");                  
                    }
                }
            }
            public MyView()
            {
                questCase = Program.GetRandomCase();
            }
            public string Question
            {
                get { return questCase.Question; }
            }
            public string Type
            {
                get { return questCase.Type; }
            }
            public string[] Answers
            {
                get { return questCase.Answers.Keys.ToArray(); }
            }

            protected void OnPropertyChanged(string propName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local ="clr-namespace:Android_1"
             x:Class="Android_1.MainPage" Title="">
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="MyDataTemplate">
                <ViewCell>
                    <Label LineBreakMode="WordWrap" Text="{Binding}"></Label>
                </ViewCell>
            </DataTemplate>
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout>
        <Label Text="{Binding Type}"/>
        <Label Text="{Binding Question}"/>
        <ListView x:Name="ListAnswers" ItemsSource="{Binding Answers}" ItemTemplate="{x:StaticResource MyDataTemplate}">
        </ListView>
        <Button Text="Next Question" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>