private void menuStrikethrough_Click(object sender, SourcedEventArgs e) { var isMenuStrikeChecked = rtb.Selection.Runs.Any() && rtb.Selection.Runs.All(r => r.TextDecorations != null && r.TextDecorations.Contains(C1TextDecorations.Strikethrough[0])); var value = isMenuStrikeChecked ? null : C1TextDecorations.Strikethrough; using (new DocumentHistoryGroup(rtb.DocumentHistory)) { var range = rtb.Selection; range.TrimRuns(); rtb.Selection = range; foreach (var r in range.EditRanges) { foreach (var run in range.Runs) { var collection = new C1TextDecorationCollection(); if (value == null) { foreach (var decoration in run.TextDecorations) { collection.Add(decoration); } collection.Remove(C1TextDecorations.Strikethrough[0]); if (collection.Count == 0) { collection = null; } } else if (run.TextDecorations == null) { collection.Add(value[0]); } else if (!run.TextDecorations.Contains(value[0])) { foreach (var decoration in run.TextDecorations) { collection.Add(decoration); } collection.Add(value[0]); } else { continue; } run.TextDecorations = null; run.TextDecorations = collection; } } rtb.Focus(); } }
private void menuUnderline_Click(object sender, SourcedEventArgs e) { var isMenuUnderlineChecked = rtb.Selection.Runs.Any() && rtb.Selection.Runs.All(r => r.TextDecorations != null && r.TextDecorations.Contains(C1TextDecorations.Underline[0])); var value = isMenuUnderlineChecked ? null : C1TextDecorations.Underline; using (new DocumentHistoryGroup(rtb.DocumentHistory)) { var range = rtb.Selection; range.TrimRuns(); rtb.Selection = range; foreach (var r in range.EditRanges) { foreach (var run in range.Runs) { var collection = new C1TextDecorationCollection(); if (value == null) { foreach (var decoration in run.TextDecorations) collection.Add(decoration); collection.Remove(C1TextDecorations.Underline[0]); if (collection.Count == 0) collection = null; } else if (run.TextDecorations == null) { collection.Add(value[0]); } else if (!run.TextDecorations.Contains(value[0])) { foreach (var decoration in run.TextDecorations) collection.Add(decoration); collection.Add(value[0]); } else { continue; } run.TextDecorations = null; run.TextDecorations = collection; } } rtb.Focus(); } }