全部。我的項目中有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("&", "&");
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,它們在呈現時都由於解析錯誤而失敗。
我甚至不確定這是否可行,因爲我突出顯示了文本並且需要選擇它。我歡迎任何建議或想法。
謝謝
你輸入的一個小樣本「HTML」(它根本不是html)真的有幫助。 – AnthonyWJones