void ForegroundOnSelectionChanged(object sender, SelectionChangedEventArgs args) { ColorGridBox clrbox = args.Source as ColorGridBox; this.txtbox.Selection.ApplyPropertyValue(FlowDocument.ForegroundProperty, clrbox.SelectedValue); }
void AddCharToolBar(ToolBarTray tray, int band, int index) { ToolBar toolbar = new ToolBar(); toolbar.Band = band; toolbar.BandIndex = index; tray.ToolBars.Add(toolbar); comboFamily = new ComboBox(); comboFamily.Width = 144; comboFamily.ItemsSource = Fonts.SystemFontFamilies; //系统的系列字体 comboFamily.SelectedItem = this.txtbox.FontFamily; comboFamily.SelectionChanged += FamilyComboOnSelection; toolbar.Items.Add(comboFamily); ToolTip tip = new ToolTip(); tip.Content = "Font Family"; comboFamily.ToolTip = tip; comboSize = new ComboBox(); comboSize.Width = 72; comboSize.IsEditable = true; comboSize.StaysOpenOnEdit = true; comboSize.IsTextSearchEnabled = false; //关闭文本搜索,防止在输入文本时自动匹配触发SelectionChanged事件 comboSize.Text = (0.75 * this.txtbox.FontSize).ToString(); comboSize.ItemsSource = new double[] { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 }; comboSize.SelectionChanged += SizeComboOnSelection; comboSize.GotKeyboardFocus += SizeComboOnGotFocus; comboSize.LostKeyboardFocus += SizeComboOnLostFocus; comboSize.PreviewKeyDown += SizeComboOnKeyDowm; toolbar.Items.Add(comboSize); tip = new ToolTip(); tip.Content = "Font Size"; comboSize.ToolTip = tip; btnBold = new ToggleButton(); btnBold.Checked += BoldButtonOnChecked; btnBold.Unchecked += BoldButtonOnChecked; toolbar.Items.Add(btnBold); Image img = new Image(); img.Source = new BitmapImage(new Uri("pack://application:,,/Images/bold.png")); img.Stretch = Stretch.None; btnBold.Content = img; tip = new ToolTip(); tip.Content = "Bold"; btnBold.ToolTip = tip; btnItalic = new ToggleButton(); btnItalic.Checked += ItalicButtonOnChecked; btnItalic.Unchecked += ItalicButtonOnChecked; toolbar.Items.Add(btnItalic); img = new Image(); img.Source = new BitmapImage(new Uri("pack://application:,,/Images/italic.png")); img.Stretch = Stretch.None; btnItalic.Content = img; tip = new ToolTip(); tip.Content = "Italic"; btnItalic.ToolTip = tip; toolbar.Items.Add(new Separator()); Menu menu = new Menu(); toolbar.Items.Add(menu); MenuItem item = new MenuItem(); menu.Items.Add(item); img = new Image(); img.Source = new BitmapImage(new Uri("pack://application:,,/Images/color_back.png")); img.Stretch = Stretch.None; item.Header = img; clrboxBackground = new ColorGridBox(); clrboxBackground.SelectionChanged += BackgroundOnSelectionChanged; item.Items.Add(clrboxBackground); tip = new ToolTip(); tip.Content = "Background Color"; item.ToolTip = tip; item = new MenuItem(); menu.Items.Add(item); img = new Image(); img.Source = new BitmapImage(new Uri("pack://application:,,/Images/color_fore.png")); img.Stretch = Stretch.None; item.Header = img; clrboxForeground = new ColorGridBox(); clrboxForeground.SelectionChanged += ForegroundOnSelectionChanged; item.Items.Add(clrboxForeground); tip = new ToolTip(); tip.Content = "Foreground Color"; item.ToolTip = tip; this.txtbox.SelectionChanged += TextBoxOnSelectionChanged; //选择文字时,更新工具栏信息 }