Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Is there a way to bind view model which has constructor with parameter?

It seems that we can't bind a view model which doesn't have a default constructor, right?
What if I do need pass extra data to construct a view model?

Best Answer

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    You can create the ViewModel and set the BindingContext of your page in the code-behind.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Is there a way to bind view model which has constructor with parameter?

    I created a basic demo about binding a view model with a parameter constructor. bind a view model with a parameter constructor. Check the code.

    public partial class Page1 : ContentPage
    {
        ObservableCollection<model_class> models = new ObservableCollection<model_class>();
        public Page1()
        {
            InitializeComponent();
    
            models.Add(new model_class("default_content") { content = "content_1" });
            models.Add(new model_class("default_content") { content = "content_2" });
            models.Add(new model_class("default_content"));
    
            listview.ItemsSource = models;
        }
    }
    
    public class model_class : INotifyPropertyChanged
    {
        public string content;
    
        public model_class(string content)
        {
            this.content = content;
        }
    
        public string Content
        {
            get
            {
                return content;
            }
            set
            {
                OnPropertyChanged();
                content = value;
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    <ListView x:Name="listview">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding Content}"/>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    
  • GarliCatGarliCat Member ✭✭
    edited October 2019
    > @Jarvan said:
    > (Quote)
    > I created a basic demo about binding a view model with a parameter constructor. bind a view model with a parameter constructor. Check the code.
    >
    > public partial class Page1 : ContentPage{ ObservableCollection<model_class> models = new ObservableCollection<model_class>(); public Page1() { InitializeComponent(); models.Add(new model_class("default_content") { content = "content_1" }); models.Add(new model_class("default_content") { content = "content_2" }); models.Add(new model_class("default_content")); listview.ItemsSource = models; }}public class model_class : INotifyPropertyChanged{ public string content; public model_class(string content) { this.content = content; } public string Content { get { return content; } set { OnPropertyChanged(); content = value; } } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }}
    >
    > <ListView x:Name="listview"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Label Text="{Binding Content}"/> </ViewCell> </DataTemplate> </ListView.ItemTemplate></ListView>

    I can understand your code but it’s not what I want.
    My page doesn’t need a collection. I just want to bind a view model as the page’s binding context.
Sign In or Register to comment.