2012-08-15 74 views
0

我從前一頁中提取數據,該數據來自wcf服務的列表框中的選定項目。WCF中的LineBreaks綁定文本塊

Ther錯誤我遇到的是textblock沒有讀取我的數據中的格式。

這是導致從以前的頁面

private void LoadPlayer() 
    { 
     FrameworkElement root1 = Application.Current.RootVisual as FrameworkElement; 
     var currentPlayer = root1.DataContext as PlayerProfile; 
     _SelectedPlayer = currentPlayer; 
    } 

這是XAML

<TextBlock Height="Auto" TextWrapping="Wrap" Name="Blurb" Text="{Binding Bio}" xml:space="preserve" /> 

具體我想獲得的\ r \ n的工作數據的代碼我顯示爲換行符。

回答

0

在這裏看到了答案:

Newline in string attribute

你的情況,你需要做的寫一個轉換器(的東西,實現的IValueConverter)這是什麼打開包含\ r \ n爲進編碼實體字符串數據即 和 。然後在你的Binding上使用該轉換器。

public class EncodeCRLFConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
    string stringtoconvert = value as string; 

    if (input != null)) 
    { 
     // Note there are different ways to do the replacement, this is 
     // just a very simplistic method. 

     stringtoconvert = stringtoconvert.Replace("\r", "&#x0d;"); 
     stringtoconvert = stringtoconvert.Replace("\n", "&#x0a;"); 

     return stringtoconvert; 
    } 
    return null; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
    throw new Exception("The method or operation is not implemented."); 
    } 
} 

在某處創建轉換器的實例...例如,通常在.Resources ....(在這個例子中,我剛剛使用了Window,因爲我不知道你的TextBlock在裏面)。

<Window.Resources> 
<EncodeCRLFConverter x:Key="strconv"/> 
<Window.Resources> 

<TextBlock Height="Auto" TextWrapping="Wrap" Name="Blurb" Text="{Binding Bio, Converter={StaticResource strconv}}" />