Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVMCross - MvxBind: Combine/Concat Properties

I have a little question:

In my axml Designer I have something like this:

<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  local:MvxBind="{'Text':{'Path':'FirstName'}}" />

That works fine, but how could I combine/concat 2 Properties (or even more)..

So something like: FirstName + SecondName (2 Properties in one Text)

like image 300
eMi Avatar asked Feb 17 '26 10:02

eMi


1 Answers

This is a standard Mvvm question. I think it came up a lot in the early years of Wpf - I think there are lots of ideas around about multiple dependencies.... I've not implemented any of these yet...

If you want to do this, then you could use:


(1) expose a combination property on the ViewModel object:

public string FullName
{
    get
    {
        return FirstName + SecondName;
    }
}

if you do this, then you'll need to make sure that when you RaisePropertyChanged("FirstName") or RaisePropertyChanged("SecondName"), then you also RaisePropertyChanged("FullName")


(2) Use a converter to combine the names together:

<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  local:MvxBind="{'Text':{'Path':'','Converter':'MakeFullName'}}" />

Note that the Converter here takes the parent object as its input parameter.

Note that in this case, if FirstName or SecondName change then the text view might not get updated :/


(3) You could use multiple TextViews in the UI - each one bound to the necessary bit of text.


(4) You could use a single textview and use C# level binding - e.g. in the View use code like:

   ViewModel.PropertyChanged += (s,e) => {
      if (e.PropertyName == "FirstName" || e.PropertyName == "SecondName")
      {
          this.FindViewById<TextView>(Resource.Id.XXX).Text = ViewModel.FirstName + ViewModel.SecondName;
      }
   }

If you think multi-dependency bindings are an important requirement, please log this as an issue (feature request) in https://github.com/slodge/MvvmCross/issues - or maybe even just fork the code and add them :)

like image 175
Stuart Avatar answered Feb 21 '26 14:02

Stuart