0
votes

I want to binding data(children) from my ViewModel to StackLayout.

In XAML I want something like:

<StackLayout Content="{Binding MainStackLayout}"/>

Is there anyway how to make this?

1
What have you tried so far? And what errors or problems are you currently experiencing?André Vermeulen
Yes, you simply need to have your ViewModel have a property of public View MainStackLayout { get; set; } (with property changed method in the setter).sme
If you put controls (your stacklayout content) in the viewmodel it really breaks the MVVM pattern. You are suppose to separate the view from the viewmodel.Ken Tucker
So should I keep stacklayout in view?Martin Zbořil
I'm not sure if you already did it, but I strongly recommend you to read the xamarin developers site article about MVVM. When you break a pattern, as @KenTucker advised you, you'll have to struggle with even bigger problems in the future, it doesn't worth..Diego Rafael Souza

1 Answers

1
votes

First of all, a StackLayout has no "Content" property. Now if you want to bind some content inside that would be in form of

<StackLayout Children="{Binding MyChildren}"/>

Your whatever BindingContext (your model) must implement the INotifyPropertyChanged interface and the variable can look like:

private List<View> _MyChildren;
public List<View> MyChildren
{
    get { return _MyChildren; }
    set
    {
        if (_MyChildren != value)
        {
            _MyChildren = value;
            OnPropertyChanged();
        }
    }
}