ichTextArea给我们提供了一个功能极丰富的文本框。
对我们以后开发在线网站内容篇写器或者聊天程序中的消息显示极为重要。以下简单介绍在实际应用中的一些我的代码,希望对大家以后的开发有所帮助。
//可以容纳任何UIElement
InlineUIContainer ic = new InlineUIContainer();
Image img = new Image();
img.Source = new BitmapImage(new Uri("icon_biggrin.png", UriKind.RelativeOrAbsolute));
ic.Child = img;
rta.Selection.Insert(ic);
//Paragraph pg = new Paragraph();
//pg.Inlines.Add("text");
////给它一条下线
//pg.TextDecorations = TextDecorations.Underline;
//rta.Blocks.Add(pg);
Hyperlink hl = new Hyperlink();
//Run可以插在任何Inlines中
hl.Inlines.Add(new Run() { Text = "this is a link" });
hl.NavigateUri = new Uri("http://funsl.com", UriKind.Absolute);
rta.Selection.Insert(hl);
---------------------------------------------------------------------------------------------------------------------------
对选中内容的操作:
if (richTextBox.Selection.Text.Length > 0)
{
//改变颜色
DependencyProperty property = TextElement.ForegroundProperty;
var value = (Brush)richTextBox.Selection.GetPropertyValue(property);
//Tag为color
var newValue = new SolidColorBrush((Color)((Rectangle)cbb_colors.SelectedItem).Tag);
richTextBox.Selection.SetPropertyValue(property, newValue);
}
-----------------------------------------------------------------------------------------------------------------------------
if (richTextBox.Selection.Text.Length > 0)
{
//缩小字体
DependencyProperty property = TextElement.FontSizeProperty;
var value = (double)richTextBox.Selection.GetPropertyValue(property);
var newValue = value - 5;
richTextBox.Selection.SetPropertyValue(property, newValue);
}
-----------------------------------------------------------------------------------------------------------------------------
if (richTextBox.Selection.Text.Length > 0)
{
//加大字体
DependencyProperty property = TextElement.FontSizeProperty;
var value = (double)richTextBox.Selection.GetPropertyValue(property);
var newValue = value + 5;
richTextBox.Selection.SetPropertyValue(property, newValue);
}
-----------------------------------------------------------------------------------------------------------------------------
//下线
DependencyProperty property = TextElement.TextDecorationsProperty;
var value = richTextBox.Selection.GetPropertyValue(property);
TextDecorationCollection newValue = TextDecorations.Underline;
if (value != null && value != DependencyProperty.UnsetValue)
{
newValue = null;
}
richTextBox.Selection.SetPropertyValue(TextElement.TextDecorationsProperty, newValue);
-----------------------------------------------------------------------------------------------------------------------------
//斜体
var boldDP = TextElement.FontWeightProperty;
var value = richTextBox.Selection.GetPropertyValue(boldDP);
var newValue = FontWeights.Bold;
if (value != DependencyProperty.UnsetValue && ((FontWeight)value) == FontWeights.Bold)
{
// Untoggle Bold if already set
newValue = FontWeights.Normal;
}
// Set new bold value
richTextBox.Selection.SetPropertyValue(boldDP, newValue);
-----------------------------------------------------------------------------------------------------------------------------
//粗体
DependencyProperty property = TextElement.FontStyleProperty;
var value = richTextBox.Selection.GetPropertyValue(property);
var newValue = FontStyles.Italic;
if (value != DependencyProperty.UnsetValue && ((FontStyle)value) == FontStyles.Italic)
{
newValue = FontStyles.Normal;
}
richTextBox.Selection.SetPropertyValue(property, newValue);
-----------------------------------------------------------------------------------------------------------------------------