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????
Hi Carles,
Is this the RichTextBox .NET provided control in WPF?
Hi Matthew Kraft
SORRY,
yes, this is the RichTextBox .NET provided control in WPF?
i need in c# not in xaml
thank you very much
Hello Carles,
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.
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
I am attaching the sample project I put together to test the binding functionality so you can use it as a reference.
In the provided code snippet above there are some instances with no definitions e.g. mytry and TextLinkUrl so it is probably some parts of the code are missing.
Anyway, if you would like to bind the Text property of the TextBox to a property in the RTBNavigationService class you can create an instance of the class and use it in the binding https://msdn.microsoft.com/en-us/library/ms742863(v=vs.100).aspx.
If you need further assistance on this matter I would suggest you to use MSDN forums for additional details on the usage of the .NET controls.
Hello Mariathank you very much for all the help you've given me,
now I have the TextBox and RichTextBox binding
all my code is:
namespace RichTextBox_Hyperlink{public partial class MainWindow : Window {public class Entry { public string MyText { get; set; } }
public MainWindow() { InitializeComponent(); CreateItems(); }
public void Change_LeaveTextbox(object sender, RoutedEventArgs e) { mytry.Content = "adios"; Entry test = new Entry(); test.MyText = ((TextBox)sender).Text;
this.DataContext = test;
if (TextWithUrl.Visibility == Visibility.Visible) { TextWithUrl.Visibility = Visibility.Hidden; TextLinkUrl.Visibility = Visibility.Visible; } }
public void TextLinkUrl_MouseLeftButtonDown(object sender, RoutedEventArgs e) {
mytry.Content = "hola"; if (!Keyboard.IsKeyDown(Key.LeftCtrl)) { if (TextLinkUrl.Visibility == Visibility.Visible) { TextWithUrl.Visibility = Visibility.Visible; TextLinkUrl.Visibility = Visibility.Hidden; } } }
private void CreateItems() { CreateTextBox(); CreateRichTextBox();}
private void CreateTextBox() // TEXT FOR COPY TO RICHTEXTBOX
{ 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); }
private void CreateRichTextBox() //TEXT COPY OF TEXTBOX { // 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.SetBinding(TextBox.TextProperty, "TextWithUrl"); txtOutPutData.Document = mcFlowDoc; txtOutPutData.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(TextLinkUrl_MouseLeftButtonDown); Stack.Children.Add(txtOutPutData); } }}
AND THE CLASS code of public static class RTBNavigationService IS:
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 void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var richTextBox = d as RichTextBox; if (richTextBox == null) return;
var newContent = (string)e.NewValue; if (string.IsNullOrEmpty(newContent)) return;
int lastPos = 0; foreach (Match match in regexUrl.Matches(newContent)) { if (match.Index != lastPos) { richTextBox.AppendText(newContent.Substring(lastPos, match.Index - lastPos)); }
var link = new Hyperlink(new Run(match.Value)) { NavigateUri = new Uri(match.Value) }; link.Click += OnUrlClick;
Paragraph para = new Paragraph(); para.Inlines.Add(link); richTextBox.Document.Blocks.Add(para);
lastPos = match.Index + match.Length; }
if (lastPos < newContent.Length) richTextBox.AppendText(newContent.Substring(lastPos)); }
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.AbsoluteUri); } }
now the problem comes when I have to pass the text of textbox for my class: "public static class RTBNavigationService" order to format the links and put the event on click ... could you tell me how can I do this?
Hi Maria
hey now gone up to 4.5.2 but still, sige without the binding work for me: S do not understand why, when you're doing run.SetBinding (Run.TextProperty, "TextWithUrl");
There declare that I want this linked element, in my case TextWithUrl, right?
public MainWindow() { InitializeComponent(); Closing += (s, e) => ViewModelLocator.Cleanup(); CreateItems(); }
private void CreateTextBox() // TEXT FOR COPY TO RICHTEXTBOX { 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); }
private void CreateRichTextBox() //TEXT COPY OF TEXTBOX {
// 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, "TextLinkUrl"); 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.SetBinding(TextBox.TextProperty, "TextWithUrl");txtOutPutData.Document = mcFlowDoc; txtOutPutData.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(TextLinkUrl_MouseLeftButtonDown); Stack.Children.Add(txtOutPutData); }
I see that the project was built against .NET3.5 and the Text property is not available there. To my knowledge it is added with .NET4 and it could be bound as well. I would suggest you to contact the MSDN support for additional details on this scenario for .NET3.5.
Please do not hesitate to let me know if you have any questions regarding the Infragistics controls.
i can try your sample project but, the RichTextBox and TextBox not has binding,
RichTextBox Hyperlink.zip