Is there a way that one can 'add' two bindings together and add some strings to them? This is quite hard to explain but one does a binding in your XAML code to a TextBlock for example like this:
<TextBlock Name="FirstName" Text="{Binding FN}" />
What I want to do is this:
<TextBlock Name="FirstLastName" Text="{Binding FN} + ', ' + {Binding LN}" />
So in essence you'll get something like this:
Dean, Grobler
Thanks in advance!
First that comes to mind is to create additional property in VM
that will contain concatenated values:
public string FullName
{
get { return FN + ", "+ LN; }
}
public string FN
{
get { return _fN; }
set
{
if(_fn != value)
{
_fn = value;
FirePropertyChanged("FN");
FirePropertyChanged("FullName");
}
}
}
public string LN
{
get { return _lN; }
set
{
if(_lN != value)
{
_lN = value;
FirePropertyChanged("LN");
FirePropertyChanged("FullName");
}
}
}
Another approach that might help is to use converter. but in this case we assume that FN
and LN
are properties of same object:
and
public class PersonFullNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is Person)) throw new NotSupportedException();
Person b = value as Person;
return b.FN + ", " + b.LN;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Person
{
public string FN { get; set; }
public string LN { get; set; }
}
and VM
:
public Person User
{
get { return _user; }
set
{
if(_user != value)
{
_user = value;
FirePropertyChanged("User");
}
}
}