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

问题描述 投票:0回答:1

我有此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>

在MainPage类C#代码中:

        public MyView answerView;
        public MainPage()
        {
            InitializeComponent();
            answerView = new MyView();
            this.BindingContext = answerView;
        }
        private void Button_Clicked(object sender, EventArgs e)
        {
             answerView.QuestCase = Program.GetRandomCase();
        }

现在就可以了。上面的代码有效。所有绑定都有效。

[当我更改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>

如何解决?谢谢。


和MyView类:

  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));
            }
        }
listview xamarin binding datatemplate itemtemplate
1个回答
0
投票
<?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>

这可行!

© www.soinside.com 2019 - 2024. All rights reserved.