没有找到'ItemsSource'的属性,可绑定属性或事件,或者值和属性之间的不匹配类型?

时间:2018-02-08 05:06:04

标签: c# xaml xamarin xamarin.forms visual-studio-2017

我正在尝试在Xamarin中实施该项目,并且我收到以下错误并且无法解决它:

找不到'ItemsSource'的属性,可绑定属性或事件,或者值和属性之间的类型不匹配?

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="HelloWorld.Exercises.AirbnbSearchList">
    <StackLayout>
        <SearchBar></SearchBar>
        <ListView  x:Name="listView"
              ItemTapped="Handle_ItemTapped"
              ItemSelected="Handle_ItemSelected"
              IsPullToRefreshEnabled="True"
              Refreshing="Handle_Refresh" 
              IsGroupingEnabled="True" 
              GroupDisplayBinding="{Binding RecentSearch}" >
            <ListView.ItemsSource>
                <DataTemplate>
                    <TextCell Text="{Binding Location}" Detail="{Binding NewProperty}">
                        <TextCell.ContextActions>
                            <MenuItem Text="Delete" Clicked="Delete_Clicked" IsDestructive="True" CommandParameter="{Binding .}"></MenuItem>
                        </TextCell.ContextActions>
                    </TextCell>
                </DataTemplate>
            </ListView.ItemsSource>
        </ListView>
    </StackLayout>
</ContentPage>

.CS代码如下:

namespace HelloWorld.Exercises
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class AirbnbSearchList : ContentPage
    {
        public SearchService ss = new SearchService();

        public AirbnbSearchList()
        {
            InitializeComponent();

            ObservableCollection<Search> list = ss.GetSearches();

            var searchGrp = new SearchGroup("Recent Searches");

            foreach (var listItem in list)
            {
                searchGrp.Add(listItem);
            }

            var searchElement = new ObservableCollection<SearchGroup>();
            searchElement.Add(searchGrp);

            listView.ItemsSource = searchElement;

        }

        void Handle_TextChanged(object sender, TextChangedEventArgs e)
        {
            listView.ItemsSource = ss.GetSearches(e.NewTextValue);
        }

        void Handle_Refresh(object sender, EventArgs e)
        {
            listView.ItemsSource = ss.GetSearches();
            listView.EndRefresh();
        }

        void Handle_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            listView.SelectedItem = null;
        }

        void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var search = e.Item as Search;
            DisplayAlert("Location", search.Location, "OK");
        }

        void Delete_Clicked(object sender, EventArgs e)
        {
            //var search = (sender as MenuItem).CommandParameter as Contact;
            //_contacts.Remove(contact);
        }
    }
}

这是我的Search.class文件:

namespace HelloWorld.Exercises.Models
{
    public class Search
    {
        public int Id;
        public string Location;
        public string CheckIn;
        public string CheckOut;


        public string NewProperty
        {

            get
            {
                return string.Format("{0} - {1} / {2}", CheckIn, CheckOut, Location);
            }
        }
    }
}

这是SearchGroup:

namespace HelloWorld.Exercises.Models
{
     class SearchGroup : List<Search>
    {
        public string RecentSearch;

       public SearchGroup(string recentSearch)
        {
            this.RecentSearch = recentSearch;
        }

    }
}

如何解决这个问题?我尝试在Google上搜索,但由于文档较少而且这是一个特定的错误,任何解决方案或提示都会有所帮助。 感谢。

1 个答案:

答案 0 :(得分:1)

您要将项目的DataTemplate设置为XAML中的项目来源,但应将其用作项目模板。在XAML文件中将ListView.ItemsSource及其结束标记更改为ListView.ItemTemplate