【发布时间】:2020-04-29 15:49:31
【问题描述】:
我需要为当前插入符号位置获取/设置当前段落(特别是行高和行间距值)。
我可以这样设置字体系列:
public string SelectionFontFamily
{
get => GetFormatting(Inline.FontFamilyProperty)?.ToString() ?? "";
set => SetFormatting(Inline.FontFamilyProperty, value ?? "Times New Roman");
}
private void SetFormatting(DependencyProperty property, object value)
{
var selection = Rtb.Selection;
selection.ApplyPropertyValue(property, value);
}
private object GetFormatting(DependencyProperty property)
{
var range = Rtb.Selection;
object value = null;
var pointer = range.Start;
if (pointer is TextPointer)
{
var needsContinue = true;
DependencyObject element = pointer.Parent as TextElement;
while (needsContinue && (element is Inline || element is Paragraph || element is TextBlock))
{
value = element.GetValue(property);
needsContinue = ((value is IEnumerable seq)) ? seq.Cast<object>().Count() == 0 : value == null;
element = element is TextElement ? ((TextElement)element).Parent : null;
}
}
return value;
}
这按预期工作。但是,我不知道如何在 line-height 和 line-spacing 方面完成同样的事情。
我找到了一个看起来至少对行高有希望的答案here
Paragraph p = Rtb.Document.Blocks.FirstBlock as Paragraph;
p.LineHeight = 10;
但我需要将此应用于选定的块,而不是第一个块。如果我能弄清楚用户用插入符号选择了哪个段落,那可能会起作用。
感谢任何帮助。 TIA。
【问题讨论】: