locked
Why does setting page's BindingContext not work? RRS feed

  • Question

  • User388603 posted

    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?

    Monday, October 14, 2019 4:06 AM

Answers

  • User369978 posted

    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 .

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 14, 2019 8:00 AM
  • User369978 posted

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

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 15, 2019 9:37 AM

All replies

  • User369978 posted

    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 .

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 14, 2019 8:00 AM
  • User388603 posted

    @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.

    Tuesday, October 15, 2019 1:53 AM
  • User388603 posted

    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.

    Tuesday, October 15, 2019 2:02 AM
  • User369978 posted

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

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 15, 2019 9:37 AM
  • User388603 posted

    @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.

    Sunday, October 20, 2019 9:41 AM
  • User369978 posted

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

    Monday, October 21, 2019 12:04 PM
  • User388603 posted

    @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.

    Saturday, October 26, 2019 12:01 PM
  • User369978 posted

    @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 .

    Monday, October 28, 2019 7:16 AM