BindingContext error

Hi,
I'm creating a page with a ListView, and after selecting one of the objects and clicking on it should bring you to a details page for the item.
Everything works from the ListView, but I get an error after it tries to apply the BindingContext to the details page after an item is selected.
I'm not sure what I'm doing wrong.
Please advise...

//ContentPage containing the ListView
public class ListingPage : ContentPage
{
private ObservableCollection users { get; set; }

public ListingPage()
{
    PreLoadData();
}

private async void PreLoadData()
{
    try
    {
        App.ContextData.Sproc = "GetMeetingInfosOSP";
        string url = App.MainConnection + App.ContextData.Sproc + "/" + App.ContextData.UserID;
        Task<bool> task = LoadData(url);
        bool data = await task;

        if (data)
        {
            await PrepareData();

            var listView = new ListView();
            listView.RowHeight = 40;
            this.Title = "Kunden Info Liste";
            listView.ItemTemplate = new DataTemplate(typeof(DataCell));
            listView.ItemsSource = users;

            listView.ItemSelected += (sender, e) => {
                UserData ud = (UserData)e.SelectedItem;
                var page = new UserDataDetailsPage(ud);

                page.BindingContext = ud;                      <=    //here it calls the error!!!!!  (System.InvalidCastException: Specified cast is not valid.)

                Navigation.PushAsync(page);
            };

            Content = listView;
        }
        else
        {
        await this.DisplayAlert("Fehler", "Die Daten konnten nicht geladen werden!", "OK");
        }
    }
    catch(Exception ex)
    {
    await this.DisplayAlert("Error", ex.Message, "OK");
    }
}

}

//the ViewCell for the ListView
public class DataCell : ViewCell
{
public DataCell()
{
//instantiate each of our views
var nameLabel = new Label();
var dateLabel = new Label();
var statusLabel = new Label();
var layout = new StackLayout();

        //set bindings
        nameLabel.SetBinding(Label.TextProperty, new Binding("Name"));
        dateLabel.SetBinding(Label.TextProperty, new Binding("Datum"));
        statusLabel.SetBinding(Label.TextProperty, new Binding("Status"));

        //Set properties for desired design
        layout.Orientation = StackOrientation.Horizontal;
        layout.HorizontalOptions = LayoutOptions.Fill;

        nameLabel.FontSize = 18;
        statusLabel.FontSize = 15;
        dateLabel.FontSize = 15;

        //add views to the view hierarchy
        layout.Children.Add(nameLabel);
        layout.Children.Add(statusLabel);
        layout.Children.Add(dateLabel);

        // add to parent view
        View = layout;
    }

}

//UserData Object for Details Page
public class UserData
{
public string Name { get; set; }
public string ID { get; set; }

public UserData()
{
    this.Name = "";
    this.ID = ""; 
}

public UserData(string name, string id)
{
    this.Name = name;
    this.ID = id;
}

}

//The details Page for the Item
public class UserDataDetailsPage : ContentPage
{
public UserDataDetailsPage()
{
this.Padding = new Thickness(0, Device.OnPlatform(0, 20, 0), 0, 0);
this.Content = SetPageLayout();
}

    private StackLayout SetPageLayout()
    {
        StackLayout layout = new StackLayout()
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Orientation = StackOrientation.Vertical
        };

        StackLayout nameStack;
        layout.Children.Add(nameStack = new StackLayout
        {
            HorizontalOptions = LayoutOptions.StartAndExpand,
            VerticalOptions = LayoutOptions.Start,
            Orientation = StackOrientation.Horizontal
        });

        nameStack.Children.Add(new Label
        {
            Text = "Name:",
            FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.Center
        });

        Label nameLbl = new Label()
        {
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.Center
        };
        nameStack.SetBinding(Label.TextProperty, "Name");
        nameStack.Children.Add(nameLbl);


        StackLayout statusStack;
        layout.Children.Add(statusStack = new StackLayout
        {
            HorizontalOptions = LayoutOptions.StartAndExpand,
            VerticalOptions = LayoutOptions.Start,
            Orientation = StackOrientation.Horizontal
        });

        statusStack.Children.Add(new Label
        {
            Text = "Status:",
            FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.Center
        });

        Label statusLbl = new Label()
        {
            HorizontalOptions = LayoutOptions.Start,
            VerticalOptions = LayoutOptions.Center
        };
        statusStack.SetBinding(Label.TextProperty, "ID");
        statusStack.Children.Add(statusLbl);

        return layout;
    }
}

Thanks for any help.

Answers

  • josemmortegajosemmortega ESUniversity ✭✭✭

    @Gruener-IT it's very weird, because BindingContext store a object so, you can't receive a InvalidCast in this line. Sounds more like the casting for SelectedItem it's wrong, maybe you have another object in SelectedItem.

    private ObservableCollection users { get; set; } I don't know with data you fill this, but I recommend you use a ObservableCollection generic --> ObservableCollection to pretty sure to get a UserData object.

    Regards

Sign In or Register to comment.