2011-06-22 31 views
1

全部。我的項目中有ContentControl,它綁定到一個返回一個帶有HTML語法的字符串的屬性。Silverlight-如何爲可選文本創建內容控制器

控制的XAML

<ContentControl Height="48" 
     Margin="100,56,223,0" 
     VerticalAlignment="Top" 
     Content="{Binding HitContext, 
     Converter={StaticResource FormatConverter}, 
     Mode=TwoWay}" 
     Foreground="White" /> 

你會發現,我對這個控制轉換器性能。本質上,我在返回字符串時評估字符串並去除html並將其替換爲xaml以突出顯示返回內的關鍵字。

下面是格式轉換器代碼:

public class HighlightConverter : IValueConverter 
{ 
    ///<summary> 
    ///Converter class used to evaluate and highlight context string results 
    ///</summary> 
    /// 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string str = value.ToString(); 

     str = str.Replace("&", "&amp;"); 
     str = str.Replace("<fragment>", " "); 
     str = str.Replace("</fragment>", " "); 
     str = str.Replace("<hilight>", "<Run Foreground=\"Gold\" FontWeight=\"ExtraBold\" FontSize=\"13\">"); 
     str = str.Replace("</hilight>", "</Run>"); 
     return XamlReader.Load("<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TextWrapping=\"Wrap\" >" + str + "</TextBlock>"); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

到目前爲止,這工作得很好。該字符串在視圖中呈現,並且具有標記「hilight」的單詞將被轉換爲在控件中呈現突出顯示的單詞作爲xaml語法。您還可以看到其他清理,例如刪除片段標記和&符號。

我在哪裏遇到問題是我需要能夠在運行時從控件中選擇文本。當您需要從UI中選擇文本時通常使用TextBox,但它不支持Run類,因此我無法將高亮格式傳遞給UIelement。我也嘗試使用RichTextBox,但我收到了xaml解析錯誤,指出無法創建控件。

我沒有看到一個鏈接在stackoverflow和silvelright.net與用戶建議將樣式應用於文本塊的類似問題。但是,由於這是在ContentControl中呈現的,因此無法設置樣式。

到目前爲止,我已經嘗試使用ViewScroller,Textbox和RichTextBox,它們在呈現時都由於解析錯誤而失敗。

我甚至不確定這是否可行,因爲我突出顯示了文本並且需要選擇它。我歡迎任何建議或想法。

謝謝

+0

你輸入的一個小樣本「HTML」(它根本不是html)真的有幫助。 – AnthonyWJones

回答

0

您想要顯示一些也可選擇的格式化文本。正確的控制是RichTextBox。我懷疑在您最初嘗試使用RichTextBox時,您未能將最終內容字符串包含在<Paragraph>..</Paragraph>元素中。 RichTextBox擁有Block元素的集合,如Paragraph,每個元素都包含文本運行。

代碼的下面MOD作品: -

public class HighlightConverter : IValueConverter 
{ 
    ///<summary> 
    ///Converter class used to evaluate and highlight context string results 
    ///</summary> 
    /// 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string str = value.ToString(); 

     str = str.Replace("&", "&amp;"); 
     str = str.Replace("<fragment>", " "); 
     str = str.Replace("</fragment>", " "); 
     str = str.Replace("<hilight>", "<Run Foreground=\"Gold\" FontWeight=\"ExtraBold\" FontSize=\"13\">"); 
     str = str.Replace("</hilight>", "</Run>"); 

     str = "<RichTextBox xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph>" 
      + str 
      + "</Paragraph></RichTextBox>"; 

     return XamlReader.Load(str); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

BTW,放下你的Mode=TwoWay上的約束力,但它是沒有必要的。

+0

謝謝安東尼,但不幸的是我確實使用了段落標記。當我嘗試呈現該控件時,我收到以下錯誤:無法將類型'System.String'的實例添加到類型'System.Windows.Documents.BlockCollection – rlcrews

0

值得信任安東尼幫助我完成這項工作。我的問題實際上與段落標籤有關。雖然我確實插入了它們,但我把它們放在了錯誤的位置。

下面列出的是更新的代碼

public class HighlightConverter : IValueConverter 
{ 
    ///<summary> 
    ///Converter class used to evaluate and highlight context string results 
    ///</summary> 
    /// 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string str = value.ToString(); 

     str = str.Replace("&", "&amp;"); 
     str = str.Replace("<fragment>", "<Paragraph>"); 
     str = str.Replace("</fragment>", "</Paragraph>"); 
     str = str.Replace("<hilight>", "<Run Foreground=\"Gold\" FontWeight=\"ExtraBold\" FontSize=\"13\">"); 
     str = str.Replace("</hilight>", "</Run>"); 
     return XamlReader.Load("<RichTextBox xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TextWrapping=\"Wrap\" >" + str + "</RichTextBox>"); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

在零下的UI有點格式化這個偉大工程。