Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 81910

Why does setting page's BindingContext not work?

$
0
0

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?


Viewing all articles
Browse latest Browse all 81910

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>