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

  • Question

  • User388603 posted

    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?

    Tuesday, October 8, 2019 2:36 PM

Answers

  • User382871 posted

    I changed the code to bind a view model with BindingContext and it works well. ``` public partial class Page1 : ContentPage { public Page1() { InitializeComponent();

        this.BindingContext = new model_class("default_content");
    }
    

    } public class model_class { public string content { get; set; }

    public modelclass(string content)
    {
        this.content = content;
    }
    

    }

    <Label Text="{Binding content}"/>
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, October 9, 2019 6:30 AM

All replies

  • User53115 posted

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

    Tuesday, October 8, 2019 2:42 PM
  • User382871 posted

    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 models = new ObservableCollection(); 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));
    }
    

    } ```

    Wednesday, October 9, 2019 5:50 AM
  • User388603 posted

    @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 models = new ObservableCollection(); public Page1() { InitializeComponent(); models.Add(new modelclass("defaultcontent") { content = "content1" }); models.Add(new modelclass("defaultcontent") { content = "content2" }); models.Add(new modelclass("defaultcontent")); listview.ItemsSource = models; }}public class modelclass : INotifyPropertyChanged{ public string content; public modelclass(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)); }}

    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.

    Wednesday, October 9, 2019 6:16 AM
  • User382871 posted

    I changed the code to bind a view model with BindingContext and it works well. ``` public partial class Page1 : ContentPage { public Page1() { InitializeComponent();

        this.BindingContext = new model_class("default_content");
    }
    

    } public class model_class { public string content { get; set; }

    public modelclass(string content)
    {
        this.content = content;
    }
    

    }

    <Label Text="{Binding content}"/>
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, October 9, 2019 6:30 AM