private void CreaTextBox() { TextBox txtInputData = new TextBox(); //txtInputData.Visibility = Visibility.Hidden; txtInputData.Name = "TextWithUrl";
txtInputData.TextWrapping = TextWrapping.Wrap; txtInputData.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden; txtInputData.AcceptsReturn = true; txtInputData.MouseLeave += new MouseEventHandler(Change_LeaveTextbox); Stack.Children.Add(txtInputData);
RichTextBox txtOutPutData = new RichTextBox(); //txtOutPutData.Visibility = Visibility.Visible; txtOutPutData.Name = "TextLinkUrl"; txtOutPutData.IsReadOnly = true; txtOutPutData.IsDocumentEnabled = true; txtOutPutData.SetValue(TextBox.TextProperty, "TextWithUrl"); //txtOutPutData.SetBinding(TextBox.TextProperty, "TextWithUrl"); txtOutPutData.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(TextLinkUrl_MouseLeftButtonDown); Stack.Children.Add(txtOutPutData); }
why the txtOutData, IS NOT BINDING????
Hello Carles,
I am attaching the sample project I put together to test the binding functionality so you can use it as a reference.
Hello Maria,
I'm trying to get is the same effect that I have here:<RichTextBox Visibility = "Visible" x: Name = "TextLinkUrl" IsReadOnly = "True" IsDocumentEnabled = local "True": RTBNavigationService.Content = "{Binding Text, ElementName = TextWithUrl}" PreviewMouseLeftButtonDown = "TextLinkUrl_MouseLeftButtonDown">
but by code, the RTBNavigationService, works correctamense me, just do not know how to apply this binding by code correctly
the RTBNavigationService code is as follows:
using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media.Imaging;
namespace RichTextBox_Hyperlink{ public static class RTBNavigationService { private static readonly Regex regexUrl = new Regex(@"(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?"); private static readonly Regex regexSmilies = new Regex(@"(:\)(?!\)))");
public static readonly DependencyProperty ContentProperty = DependencyProperty.RegisterAttached( "Content", typeof(string), typeof(RTBNavigationService), new PropertyMetadata(null, OnContentChanged) );
public static string GetContent(DependencyObject d) { return d.GetValue(ContentProperty) as string; }
public static void SetContent(DependencyObject d, string value) { d.SetValue(ContentProperty, value); }
private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { RichTextBox richTextBox = d as RichTextBox; if (richTextBox == null) return;
string content = (string)e.NewValue; if (string.IsNullOrEmpty(content)) return;
richTextBox.Document.Blocks.Clear();
int lastPos = 0; Paragraph block = new Paragraph(); foreach (Match match in regexSmilies.Matches(content)) { if (match.Index != lastPos) block.Inlines.Add(content.Substring(lastPos, match.Index - lastPos));
BitmapImage bitmapSmiley = new BitmapImage(new Uri("giggle.gif", UriKind.Relative)); Image smiley = new Image(); smiley.Source = bitmapSmiley; smiley.Width = bitmapSmiley.Width; smiley.Height = bitmapSmiley.Height; block.Inlines.Add(smiley);
lastPos = match.Index + match.Length; } if (lastPos < content.Length) block.Inlines.Add(content.Substring(lastPos)); richTextBox.Document.Blocks.Add(block);
List<Hyperlink> results = new List<Hyperlink>(); //foreach (Match match in regexUrl.Matches(content.Replace(Environment.NewLine,""))) foreach (Match match in regexUrl.Matches(content)) { TextPointer p1 = richTextBox.ToTextPointer(match.Index); TextPointer p2 = richTextBox.ToTextPointer(match.Index + match.Length); if (p1 == null || p2 == null) { //Donothing } else { var link = new Hyperlink(p1, p2); try { if (p2.GetTextInRun(LogicalDirection.Backward).ToString().Substring(0,7) == "http://") { link.NavigateUri = new Uri(p2.GetTextInRun(LogicalDirection.Backward).ToString()); } else { link.NavigateUri = new Uri("http://"+p2.GetTextInRun(LogicalDirection.Backward).ToString()); } } catch { } link.Click += OnUrlClick; } } }
private static void OnUrlClick(object sender, RoutedEventArgs e) { if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { var link = (Hyperlink)sender; Process.Start(link.NavigateUri.ToString()); } }
public static TextPointer ToTextPointer(this RichTextBox rtb, int index) { int count = 0; TextPointer position = rtb.Document.ContentStart.GetNextContextPosition(LogicalDirection.Forward).GetNextContextPosition(LogicalDirection.Forward); while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); int length = textRun.Length; if (count + length > index) { return position.GetPositionAtOffset(index - count); } count += length; } position = position.GetNextContextPosition(LogicalDirection.Forward); } return null; }
}}
on the other hand, I'm testing the code you've told me, and I indicates that the run does not contain the definition of TextProperty, so I'm going to dig a little about it
thank you very much
The .NET RichTextBox for WPF could not be directly bound. You can use Run to add the binding as this is a DependencyProperty since .NET4 https://social.msdn.microsoft.com/Forums/vstudio/en-US/f77c011a-0aba-449f-b6f4-920e58ebf997/binding-and-richtextbox?forum=wpf.
// Create a FlowDocument FlowDocument mcFlowDoc = new FlowDocument();
// Create a paragraph with text Paragraph para = new Paragraph();
// Create a Run and bind its text Run run = new Run(); run.SetBinding(Run.TextProperty, "MyText");
para.Inlines.Add(run);
// Add the paragraph to blocks of paragraph mcFlowDoc.Blocks.Add(para);
RichTextBox txtOutPutData = new RichTextBox(); //txtOutPutData.Visibility = Visibility.Visible; txtOutPutData.Name = "TextLinkUrl"; txtOutPutData.IsReadOnly = true; txtOutPutData.IsDocumentEnabled = true; txtOutPutData.Document = mcFlowDoc; Stack.Children.Add(txtOutPutData);
You can refer to MSDN forums for additional details on the controls provided with the .NET framework.
Please feel free to let me know if you have any questions related to Infragistics products.
Hi Matthew Kraft
SORRY,
yes, this is the RichTextBox .NET provided control in WPF?
i need in c# not in xaml
Hi Carles,
Is this the RichTextBox .NET provided control in WPF?