Wpf IsHyphenationEnabled更改时未更新FlowDocument呈现
当属性Wpf IsHyphenationEnabled更改时未更新FlowDocument呈现,wpf,data-binding,flowdocument,Wpf,Data Binding,Flowdocument,当属性IsHyphenationEnabled和IsOptimalParagraphEnabled更改为在屏幕截图和下面的代码中可见时,MyFlowDocument不会更新: 我不认为属性是在文档第一次显示时静态定义的。我错过了什么吗?只是意识到我的属性不是使用绑定对象绑定的。如果有人犯了同样的初学者错误: public ViewModel () { Document = new FlowDocument { ColumnWidth = Double.M
IsHyphenationEnabled
和IsOptimalParagraphEnabled
更改为在屏幕截图和下面的代码中可见时,MyFlowDocument
不会更新:
我不认为属性是在文档第一次显示时静态定义的。我错过了什么吗?只是意识到我的属性不是使用绑定对象绑定的。如果有人犯了同样的初学者错误:
public ViewModel () {
Document = new FlowDocument {
ColumnWidth = Double.MaxValue,
TextAlignment = TextAlignment.Justify,
FontFamily = new FontFamily ("Arial"),
};
Document.SetBinding (FlowDocument.IsHyphenationEnabledProperty, new Binding ("Hyphenation"));
Document.SetBinding (FlowDocument.IsOptimalParagraphEnabledProperty, new Binding ("OptimalParagraphs"));
也许这可以简化/优化
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace FlowDocumentDemo {
public partial class MainWindow : Window { public MainWindow () { InitializeComponent (); } }
class ViewModel : INotifyPropertyChanged {
protected FlowDocument document;
protected bool hyphenation = true;
protected bool optimalParagraphs = true;
public event PropertyChangedEventHandler PropertyChanged;
public FlowDocument Document { get => document; set { document = value; RaisePropertyChanged (); } }
public bool Hyphenation { get => hyphenation; set { hyphenation = value; RaisePropertyChanged (); } }
public bool OptimalParagraphs { get => optimalParagraphs; set { optimalParagraphs = value; RaisePropertyChanged (); } }
public ViewModel () {
Document = new FlowDocument {
ColumnWidth = Double.MaxValue,
TextAlignment = TextAlignment.Justify,
FontFamily = new FontFamily ("Arial"),
IsHyphenationEnabled = Hyphenation,
IsOptimalParagraphEnabled = OptimalParagraphs
};
Paragraph paragraph = new Paragraph ();
paragraph.Inlines.Add (new Run {
FontSize = 35,
FontWeight = FontWeights.Bold,
Text = "NASA announces funding for moon and Mars mission tech"
});
Document.Blocks.Add (paragraph);
paragraph = new Paragraph ();
paragraph.Inlines.Add (new Run {
FontFamily = new FontFamily ("Arial"),
Text = "NASA announced agreements worth a combined $43.2 million with 14 commercial partners " +
"Friday — including Blue Origin and SpaceX — to fund experiments in propellant and power " +
"generation, in-space refueling, efficient propulsion systems, and lunar rover technology."
});
Document.Blocks.Add (paragraph);
}
protected void RaisePropertyChanged ([CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
}
}
}
public ViewModel () {
Document = new FlowDocument {
ColumnWidth = Double.MaxValue,
TextAlignment = TextAlignment.Justify,
FontFamily = new FontFamily ("Arial"),
};
Document.SetBinding (FlowDocument.IsHyphenationEnabledProperty, new Binding ("Hyphenation"));
Document.SetBinding (FlowDocument.IsOptimalParagraphEnabledProperty, new Binding ("OptimalParagraphs"));