最初に頭に浮かぶのはVM
、連結された値を含む追加のプロパティを作成することです。
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");
}
}
}
役立つかもしれない別のアプローチは、コンバーターを使用することです。ただし、この場合、 とが同じオブジェクトのプロパティであるFN
と仮定します。LN
と
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; }
}
とVM
:
public Person User
{
get { return _user; }
set
{
if(_user != value)
{
_user = value;
FirePropertyChanged("User");
}
}
}