Alan2
Alan2

Reputation: 24592

How can I bind to a view model in C# Xamarin Forms?

My C# code is like this now:

 stack.Children.Add(new FooterTemplate() { Text = Japanese.Helpers.Deck.SetIntro() });

but I would like to get the value of Text from my viewModel:

 vm.IntroFooter = Japanese.Helpers.Deck.SetIntro();

How can I bind the Text to vm.IntroFooter in C#

Upvotes: 0

Views: 49

Answers (1)

TaiT's
TaiT's

Reputation: 3216

Here's how to bind a property in C#:

FooterTemplate ft = new FooterTemplate();
Binding binding = new Binding("IntroFooter");
ft.SetBinding(FooterTemplate.TextProperty, binding);
stack.Children.Add(ft);

I am assuming that you are properly using INotifyPropertyChanged interface:

...
string _introFooter;
public string IntroFooter 
{
    get
    {
        return this._introFooter;
    }
    set
    {
        if (value != this._introFooter)
        {
            this._introFooter = value;
            NotifyPropertyChanged();
        }
    }
}

Upvotes: 2

Related Questions