首先想到的是創造附加屬性將包含連接值:
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; }
}
和:
public Person User
{
get { return _user; }
set
{
if(_user != value)
{
_user = value;
FirePropertyChanged("User");
}
}
}
我想這也。然而,這兩個問題的主要問題是在換成文本框的情況下轉換回來。這只是真的。 – Poken1151