jueves, 30 de abril de 2009

Rich Text box en WPF

Para los que quisieran construir o implementar un editor enriquecido de texto incluyendo imagenes en WPF, el framework 3.5 SP1 ya nos ofrece dicho editor, que por supuesto en WinForms esto ya estaba disponible.

Es un simple editor de texto que lo definimos dentro de xaml como sigue :

[RichTextBox Height="191" Margin="0,0,0,42" Width="518" Name="textAll"/]

Este editor soporta inserciones de imagenes, estilos de texto y más... y lo interesante es que dicho contenido se puede exportar a un fichero .txt , .rtf, .doc ..... etc.

La siguiente fraccion de codigo inserta una imagen a la vez, dicho evento es activado al hacer click en algun boton llamado button2.

private void button2_Click(object sender, RoutedEventArgs e)
{
TextPointer tp = textAll.CaretPosition;
//textAll.Selection;

Image img = new Image();
img.Width = 25;
img.Height = 25;
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg", UriKind.RelativeOrAbsolute);
myBitmapImage.EndInit();
img.Source = myBitmapImage;
InlineUIContainer iu = new InlineUIContainer(img);
tp.Paragraph.Inlines.Add(iu);
}

No hay comentarios: