public static RichTextBox CreateRichTextBox(RoutedEventHandler contextMenuEventHandler_Click, DragEventHandler richTextBoxEventHandler_DragOver, DragEventHandler richTextBoxEventHandler_DragDrop, TextChangedEventHandler richTextBoxEventHandler_OnTextChange ) { //---------------- Создание контекстного меню ContextMenu contextMenu = new ContextMenu(); MenuItem item = new MenuItem { Header = "Close" }; item.Click += contextMenuEventHandler_Click; contextMenu.Items.Add(item); //---------------- //---------------- Создание пустого rich text box RichTextBox richTextBox = new RichTextBox() { Name = "tmpRichText", }; //---------------- //---------------- Задание drag and drop richTextBox.AllowDrop = true; richTextBox.AddHandler(RichTextBox.DragOverEvent, richTextBoxEventHandler_DragOver, true); richTextBox.AddHandler(RichTextBox.DropEvent, richTextBoxEventHandler_DragDrop, true); //---------------- //---------------- Назначение контекстного меню/ прокрутки richTextBox.ContextMenu = contextMenu; richTextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; richTextBox.TextChanged += richTextBoxEventHandler_OnTextChange; return(richTextBox); }
private void NewFile(string fileName) { string temp = "rtbEtitor" + (items++).ToString(); // Get New File Tab Name fileName = Path.GetFileName(fileName); RichTextBox newRtb = new RichTextBox { Name = temp }; TabItem newItem = new TabItem { Header = fileName, Name = temp, Content = newRtb }; rtbEditor = newRtb; // Set the current tab Editor rtbEditor.AddHandler(RichTextBox.DropEvent, new DragEventHandler(rtbEditor_Drop), true); rtbEditor.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(rtbEditor_DragOver), true); tabControl.Items.Add(newItem); // TabController add this tab newItem.IsSelected = true; // Select this tab }
public RichTextEditorSample() { InitializeComponent(); //Initialize the original window from .xaml file. rtbEditor = rtbEditor1; // Set the current tab Editor cmbFontFamily.ItemsSource = Fonts.SystemFontFamilies.OrderBy(f => f.Source); // Initialize the Font Families cmbFontSize.ItemsSource = new List <double>() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 }; // Initialize the Font Size cmbColour.ItemsSource = typeof(Colors).GetProperties(); //Initiallze the Colour tab rtbEditor.AddHandler(RichTextBox.DropEvent, new DragEventHandler(rtbEditor_Drop), true); //Bind the Drop function rtbEditor.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(rtbEditor_DragOver), true); // Ibid. }
private TabItem AddTabItem() { int count = _tabItems.Count; // create new tab item TabItem tab = new TabItem(); tab.Header = string.Format("Tab {0}", count); tab.Name = string.Format("tab{0}", count); tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate; // add controls to tab item, this case I added just a textbox //<RichTextBox x:Name="richtextBox1" PreviewKeyDown="richtextBox1_PreviewKeyDown" AllowDrop="True" Block.LineStackingStrategy="BlockLineHeight"/> RichTextBox rich = new RichTextBox(); rich.PreviewKeyDown += richtextBox1_PreviewKeyDown; rich.AllowDrop = true; rich.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(RichTextBox_DragOver), true); rich.AddHandler(RichTextBox.DropEvent, new DragEventHandler(RichTextBox_Drop), true); rich.Name = "richtextBox1"; rich.PreviewKeyDown += richtextBox1_PreviewKeyDown; rich.AllowDrop = true; rich.Document.Blocks.Clear(); riches.Add(rich); tab.Content = riches[u]; u++; //TextBox txt = new TextBox(); //txt.Name = "txt"; //tab.Content = txt; // insert tab item right before the last (+) tab item _tabItems.Insert(count - 1, tab); return(tab); }
public override void Instantiate() { base.Instantiate(); this.RichTextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; this.RichTextBox.SetValue(this.EditingElement.Platform.Metadata.DefaultTypeResolver, ControlElement.ForegroundProperty, this.TextSource.GetComputedValueAsWpf(TextBlockElement.ForegroundProperty)); if (this.EditingElement.Platform.Metadata.ResolveProperty(TextBlockElement.BackgroundProperty) != null) { this.RichTextBox.SetValue(this.EditingElement.Platform.Metadata.DefaultTypeResolver, ControlElement.BackgroundProperty, this.TextSource.GetComputedValueAsWpf(TextBlockElement.BackgroundProperty)); } TextAlignment textAlignment = (TextAlignment)this.TextSource.GetComputedValueAsWpf(TextBlockElement.TextAlignmentProperty); TextDecorationCollection decorationCollection = (TextDecorationCollection)this.TextSource.GetComputedValueAsWpf(TextBlockElement.TextDecorationsProperty); Paragraph paragraph = (Paragraph)((FlowDocument)this.RichTextBox.Document.PlatformSpecificObject).Blocks.FirstBlock; paragraph.TextAlignment = textAlignment; paragraph.TextDecorations = decorationCollection; if (this.TextSource.IsSet(TextBlockElement.TextProperty) == PropertyState.Set) { Run run = new Run((string)this.TextSource.GetComputedValue(TextBlockElement.TextProperty)); run.FlowDirection = (FlowDirection)FrameworkElement.FlowDirectionProperty.DefaultMetadata.DefaultValue; paragraph.Inlines.Add((Inline)run); } else if (this.TextSource.IsSet(TextBlockElement.InlinesProperty) == PropertyState.Set) { DocumentNodePath pathInContainer = this.TextSource.DocumentNodePath.GetPathInContainer(((DocumentCompositeNode)this.TextSource.DocumentNode).Properties[TextBlockElement.InlinesProperty]); SceneViewModel viewModel = this.TextSource.ViewModel; IDocumentContext documentContext = viewModel.Document.DocumentContext; if (!viewModel.ProjectContext.IsCapabilitySet(PlatformCapability.IsWpf)) { foreach (SceneNode sceneNode in (IEnumerable <SceneNode>) this.TextSource.GetCollectionForProperty(TextBlockElement.InlinesProperty)) { Inline inline = this.TextSource.ViewModel.DefaultView.ConvertToWpfValue((object)sceneNode.DocumentNode) as Inline; if (inline != null) { paragraph.Inlines.Add(inline); } } } else { using (InstanceBuilderContext instanceBuilderContext = new InstanceBuilderContext(viewModel.ProjectContext, viewModel, true, (Microsoft.Expression.DesignModel.DocumentModel.DocumentNode)null)) { using (instanceBuilderContext.DisablePostponedResourceEvaluation()) { instanceBuilderContext.ViewNodeManager.RootNodePath = pathInContainer; instanceBuilderContext.ViewNodeManager.Root.Instance = (object)paragraph.Inlines; instanceBuilderContext.ViewNodeManager.Instantiate(instanceBuilderContext.ViewNodeManager.Root); } this.UpdateUIChildrenInstances((IInstanceBuilderContext)instanceBuilderContext); } } } if (this.ForceLoadOnInstantiate) { this.EditingElement.UpdateLayout(); this.AdjustPadding(); } else { this.RichTextBox.Loaded += new RoutedEventHandler(this.RichTextBox_Loaded); } if (this.TextSource.ViewModel.ProjectContext.IsCapabilitySet(PlatformCapability.SupportsTabInTextControl)) { RichTextBox richTextBox = this.RichTextBox.PlatformSpecificObject as RichTextBox; if (richTextBox != null) { richTextBox.AddHandler(CommandManager.PreviewCanExecuteEvent, (Delegate) new CanExecuteRoutedEventHandler(TextBlockEditProxy.CanExecute)); } } this.RichTextBox.SelectionChanged += new RoutedEventHandler(this.RichTextBox_SelectionChanged); }
public static void OnPlaceholderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is PasswordBox) { PasswordBox txt = d as PasswordBox; if (txt == null || e.NewValue.ToString().Trim().Length == 0) { return; } RoutedEventHandler loadHandler = null; loadHandler = (s1, e1) => { txt.Loaded -= loadHandler; var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } if (txt.Password.Length == 0) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } }; txt.Loaded += loadHandler; txt.PasswordChanged += (s1, e1) => { bool isShow = txt.Password.Length == 0; var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } if (isShow) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } else { Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } } }; } else if (d is TextBox) { TextBox txt = d as TextBox; if (txt == null || e.NewValue.ToString().Trim().Length == 0) { return; } RoutedEventHandler loadHandler = null; loadHandler = (s1, e1) => { var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } else { txt.Loaded -= loadHandler; } Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } if (txt.Text.Length == 0) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } }; txt.Loaded += loadHandler; txt.TextChanged += (s1, e1) => { bool isShow = txt.Text.Length == 0; var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } if (isShow) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } else { Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } } }; } else if (d is ComboBox) { ComboBox txt = d as ComboBox; if (txt == null || e.NewValue.ToString().Trim().Length == 0) { return; } RoutedEventHandler loadHandler = null; loadHandler = (s1, e1) => { txt.Loaded -= loadHandler; var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } if (txt.Text == null || txt.Text.Length == 0) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } }; txt.Loaded += loadHandler; RoutedEventHandler textChangedHandler = (s1, e1) => { bool isShow = txt.Text != null && txt.Text.Length == 0; var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } if (isShow) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } else { Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } } }; txt.AddHandler(System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent, textChangedHandler); } else if (d is RichTextBox) { RichTextBox txt = d as RichTextBox; if (e.NewValue == null) { return; } if (txt == null || e.NewValue.ToString().Trim().Length == 0) { return; } RoutedEventHandler loadHandler = null; loadHandler = (s1, e1) => { txt.Loaded -= loadHandler; var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } TextRange textRange = new TextRange(txt.Document.ContentStart, txt.Document.ContentEnd); if (txt.Document == null || string.IsNullOrWhiteSpace(textRange.Text)) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } }; txt.Loaded += loadHandler; RoutedEventHandler textChangedHandler = (s1, e1) => { // bool isShow = txt.Document != null && txt.Document.Blocks.Count == 0; TextRange textRange = new TextRange(txt.Document.ContentStart, txt.Document.ContentEnd); bool isShow = string.IsNullOrWhiteSpace(textRange.Text); var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } if (isShow) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } else { Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } } }; txt.AddHandler(System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent, textChangedHandler); RoutedEventHandler getFocusedChangedHandler = (s1, e1) => { // bool isShow = txt.Document != null && txt.Document.Blocks.Count == 0; TextRange textRange = new TextRange(txt.Document.ContentStart, txt.Document.ContentEnd); bool isShow = string.IsNullOrWhiteSpace(textRange.Text); var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } if (isShow) { Adorner[] ar = lay.GetAdorners(txt); if (ar != null) { for (int i = 0; i < ar.Length; i++) { if (ar[i] is PlaceholderAdorner) { lay.Remove(ar[i]); } } } } }; txt.AddHandler(System.Windows.Controls.Primitives.TextBoxBase.GotFocusEvent, getFocusedChangedHandler); RoutedEventHandler lostFocusedChangedHandler = (s1, e1) => { // bool isShow = txt.Document != null && txt.Document.Blocks.Count == 0; TextRange textRange = new TextRange(txt.Document.ContentStart, txt.Document.ContentEnd); bool isShow = string.IsNullOrWhiteSpace(textRange.Text); var lay = AdornerLayer.GetAdornerLayer(txt); if (lay == null) { return; } if (isShow) { lay.Add(new PlaceholderAdorner(txt, e.NewValue.ToString())); } }; txt.AddHandler(System.Windows.Controls.Primitives.TextBoxBase.LostFocusEvent, lostFocusedChangedHandler); } }