2
votes

My modelView:

public string Status {
    get { return _status; }
    set {
        if (value == _status) {
            return;
        }
        _status = value;
        OnPropertyChanged ("Status");
    }

My View:

Label labelStatus = new Label { 
    TextColor = Color.Green,
    FontSize = 20d
    };
    labelStatus.SetBinding (Label.TextProperty, "Status");

Then I want to present the status using something like:

string presentStatus = string.Format("Your status is {0}...", labelStatus);
Label yourStatus = new Label{Text=presentStatus}

But that doesn't really work. Nor does using

string presentStatus = string.Format("Your status is {0}...", SetBinding(Label.TextProperty,"Status"));

So how should I do to add my bound values with more text before presenting them for the user in a view.

If using XAML (which i don't), it seems possible according to: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/data_binding_basics/

2
are you setting BindingContext anywhere?Jason
Yes, just presenting the labelstatus alone work just fine, but when i try to add text around it, it fails.Pierre

2 Answers

3
votes

Xamarin Forms binding implementation doesn't currently allow complex binding scenarios like embedding bound text within static text.

There are two options

a. use multiple labels - one with the static text, one with the bound text

b. use a property on your ViewModel that concatenates the text for you

public string StatusText 
{   
  get
  {
      return string.Format("Your status is {0}...", Status);   
  }
}


public string Status {
    get { return _status; }
    set {
        if (value == _status) {
            return;
        }
        _status = value;
        OnPropertyChanged ("Status");
        OnPropertyChanged ("StatusText");
    }
1
votes

You can do that in the BindingContextChanged-event:

labelStatus.BindingContextChanged += (sender, e) =>
{
// Here you can change the Text dynamically
// E.G. labelStatus.text = "Title: " + labelStatus.text
};