2012-10-10 21 views
1

有沒有辦法可以將兩個綁定「添加」在一起並向它們添加一些字符串?這是非常難以解釋,但一個人一個在你的XAML代碼給TextBlock例如這樣的結合:Textblock上的多個綁定,包括將字符串添加到綁定的末尾

<TextBlock Name="FirstName" Text="{Binding FN}" /> 

我想要做的是這樣的:

<TextBlock Name="FirstLastName" Text="{Binding FN} + ', ' + {Binding LN}" /> 

所以在本質上你」會得到這樣的事情:

院長,Grobler

謝謝前進!

回答

3

首先想到的是創造​​附加屬性將包含連接值:

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"); 
     } 
    } 
} 

另一種方法,可以幫助是使用轉換器。但在這種情況下,我們假設FNLN是同一對象的屬性:

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");    
     } 
    } 
} 
+0

我想這也。然而,這兩個問題的主要問題是在換成文本框的情況下轉換回來。這只是真的。 – Poken1151