private void ToggleAttribute <T> (Action <T, bool> toggle, ToolbarButton button) where T : TextAttribute, new() { var attr = new T(); var isChecked = button.IsChecked.HasValue && button.IsChecked.Value; toggle(attr, isChecked); TextViewer.SetAttribute(attr); }
protected virtual void Compose() { BoldButton = new ToolbarButton { IsCheckable = true }; BoldCommand = new CommandView { Action = s => ToggleAttribute <FontWeightTextAttribute> ( (a, bold) => a.Weight = (bold ? FontWeight.Bold : FontWeight.Normal), BoldButton), Image = Iconery.FontBoldIcon, Size = DefaultSize, ToolTipText = "Bold" }; BoldButton.SetCommand(BoldCommand); ItalicButton = new ToolbarButton { IsCheckable = true }; ItalicCommand = new CommandView { Action = s => ToggleAttribute <FontStyleTextAttribute> ( (a, italic) => a.Style = (italic ? FontStyle.Italic : FontStyle.Normal), ItalicButton), Image = Iconery.FontItalicIcon, Size = DefaultSize, ToolTipText = "Italic" }; ItalicButton.SetCommand(ItalicCommand); StrikeThroughButton = new ToolbarButton { IsCheckable = true }; StrikeThroughCommand = new CommandView { Action = s => ToggleAttribute <StrikethroughTextAttribute> ( (a, value) => a.Strikethrough = value, StrikeThroughButton), Image = Iconery.FontStrikeThroughIcon, Size = DefaultSize, ToolTipText = "StrikeThrough" }; StrikeThroughButton.SetCommand(StrikeThroughCommand); UnderlineButton = new ToolbarButton { IsCheckable = true }; UnderlineCommand = new CommandView { Action = s => ToggleAttribute <UnderlineTextAttribute> ( (a, value) => a.Underline = value, UnderlineButton), Image = Iconery.FontUnderlineIcon, Size = DefaultSize, ToolTipText = "Underline" }; UnderlineButton.SetCommand(UnderlineCommand); FontFamilyCombo = new ComboBox { Width = 100 }; Font.AvailableFontFamilies.ForEach(f => FontFamilyCombo.Items.Add(f)); FontFamilyCombo.SelectionChanged += (s, e) => { var attr = new FontDataAttribute { FontFamily = FontFamilyCombo.SelectedItem as string }; TextViewer.SetAttribute(attr); }; var fontFamilyComboHost = new ToolbarItemHost { Child = FontFamilyCombo }; FontSizeCombo = new ComboBox { Width = 50 }; new int[] { 6, 8, 10, 12, 14, 16, 18, 24, 32 } .ForEach(s => FontSizeCombo.Items.Add(s.ToString())); FontSizeCombo.SelectionChanged += (s, e) => { var i = -1d; if (FontSizeCombo.SelectedItem != null && double.TryParse(FontSizeCombo.SelectedItem.ToString(), out i)) { var attr = new FontDataAttribute { FontSize = i }; TextViewer.SetAttribute(attr); } }; var fontSizeComboHost = new ToolbarItemHost { Child = FontSizeCombo }; this.AddItems(BoldButton, ItalicButton, UnderlineButton, StrikeThroughButton, fontFamilyComboHost, fontSizeComboHost); }