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.
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})*)?");
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) { var richTextBox = d as RichTextBox; if (richTextBox == null) return;
richTextBox.Document.Blocks.Clear();
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