One way is to let the textblock bind directly to OfficeDisplayName
and then put the concatenation logic in the OfficeDisplayName
property on your viewmodel instead of in the MultiValueConverter
. So when ever one of the properties FirstName, LastName, or office location change you'd fire the PropertyChanged
event for OfficeDisplayName
- i.e. something along the following lines. This way you will not need a converter at all:
class YourViewModel : ViewModel
{
string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
if (_firstName != value)
{
_firstName = value;
OnPropertyChanged("FirstName");
OnPropertyChanged("OfficeDisplayName");
}
}
}
// More properties here
// ...
public string OfficeDisplayName
{
get { return String.Join(" ", new string[] { _firstName, _lastName, _officeLocation}); }
}
}
Another way is to pass your viewmodel itself as a parameter to your MultiValueConverter
. In your converter you can set the value of OfficeDisplayName
directly. I think this way is a bit "hack-ish" but it is a matter of taste. Your code would look like the following:
The binding in XAML:
<MultiBinding Converter="{StaticResource theConverter}" Mode="OneWay">
<Binding /> <!-- Pass the datacontext as the first parameter -->
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
The converter:
class TheMultiValueConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var viewModel = values[0] as TheViewModel;
var ret = String.Join(" ", values.Skip(1).Cast<string>().ToArray());
viewModel.OfficeDisplayName = ret;
return ret;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}