Forum Xamarin.Forms

Why does setting page's BindingContext not work?

I'm trying to bind my view model to my page, which is simple like this:

public partial class DetailPage : ContentPage
    {
        public DetailPage ()
        {
            InitializeComponent();            
        }

        public DetailPage(Event _event)
        {
            InitializeComponent();
            BindingContext = new DetailPageViewModel(_event);

        }
    }

    public class DetailPageViewModel: INotifyPropertyChanged
    {
        private Event _event;
        public Event Event 
        {
            set
            {
                _event = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Event)));
            }
            get => _event;
        }
        public DetailPageViewModel(Event theEvent)
        {
            Event = theEvent;
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }

Event is a simple model class in my case, it has a Name property.

I just wanna bind its name to a label.

<Label x:Name="nameLabel" Text="{Binding Event.Name}" FontSize="Title"/>

AfterI start debugging, I cannot see the content bound to the view. I get a error in the output.
Binding: 'Event' property not found on 'Eventuality.Event', target property: 'Xamarin.Forms.Label.Text'

Why does this happen? Didn't I bind the view model rather than an Event? Did I bind it in a wrong way?

What should I do if I want to bind a view model for the whole page?

Best Answers

Answers

  • GarliCatGarliCat Member ✭✭

    @ColeX said:
    There is another Event class existing in your project , the compile does not know which Event should be taken .

    Change the class name of your custom Event , give it a strong Name MyEvent and try again .

    Thank you for your reply. I reviewed my code and I'm sure there's not any other type named Event.

    I tried as what you suggested, I changed the name of the Event type, even changed the view model's Event property name. I still get this error.

  • GarliCatGarliCat Member ✭✭

    And I found it works if I set the binding as {Binding Name} instead of {Binding Event.Name}.

    But I think I'm binding a view model and not an Event if I'm not wrong...I'm kinda confused.

  • GarliCatGarliCat Member ✭✭

    @ColeX said:
    According to the binding path , it should be Event.Name not Name , your code works fine on my side ,check my sample .

    This is weird. It's supposed to work like that. Anyway, I'm gonna review my code again to find out what's been wrong.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 2019

    Have you tried my project ? Did it work or not ?

  • GarliCatGarliCat Member ✭✭
    edited October 2019

    @ColeX said:
    Have you tried my project ? Did it work or not ?

    Sorry to reply late. Your project works well. But I just don't know why mine doesn't work.😂

    Update: Finally I got some time to check the code. And I found that this happened because another guy set the binding context to something else in somewhere else in the code and I wasn't told about that. WTF! 🙃

    Whatever. Thank you bro!, very much.

  • ColeXColeX Member, Xamarin Team Xamurai

    @GarliCat said:

    @ColeX said:
    Have you tried my project ? Did it work or not ?

    Sorry to reply late. Your project works well. But I just don't know why mine doesn't work.😂

    Update: Finally I got some time to check the code. And I found that this happened because another guy set the binding context to something else in somewhere else in the code and I wasn't told about that. WTF! 🙃

    Whatever. Thank you bro!, very much.

    Please mark it as answered .

Sign In or Register to comment.