私はあなたに問題のない解決策を提供することができ、あなたはそれを受け入れることができますが、その前に、Document がそもそもなぜではないのかを説明しようと思いますDependencyProperty。
RichTextBoxコントロールの有効期間中、Documentプロパティは通常変更されません。はRichTextBoxで初期化されますFlowDocument。そのドキュメントは表示され、さまざまな方法で編集およびマングルできますが、Documentプロパティの基になる値は の 1 つのインスタンスのままFlowDocumentです。DependencyPropertyしたがって、これを Bindable にする必要はまったくありません。この を参照する場所が複数ある場合FlowDocument、参照が必要なのは 1 回だけです。どこでも同じインスタンスであるため、変更は誰でもアクセスできます。
FlowDocumentよくわかりませんが、ドキュメント変更通知をサポートしているとは思いません。
そうは言っても、ここに解決策があります。開始する前に、RichTextBoxは実装されておらずINotifyPropertyChanged、 Document は ではないため、 の Document プロパティが変更さDependencyPropertyれたときに通知がないRichTextBoxため、バインディングは OneWay にしかできません。
を提供するクラスを作成しますFlowDocument。バインドには が存在する必要があるDependencyPropertyため、このクラスは から継承されDependencyObjectます。
class HasDocument : DependencyObject
{
public static readonly DependencyProperty DocumentProperty =
DependencyProperty.Register("Document",
typeof(FlowDocument),
typeof(HasDocument),
new PropertyMetadata(new PropertyChangedCallback(DocumentChanged)));
private static void DocumentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("Document has changed");
}
public FlowDocument Document
{
get { return GetValue(DocumentProperty) as FlowDocument; }
set { SetValue(DocumentProperty, value); }
}
}
WindowXAML でリッチ テキスト ボックスを使用して を作成します。
<Window x:Class="samples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Flow Document Binding" Height="300" Width="300"
>
<Grid>
<RichTextBox Name="richTextBox" />
</Grid>
</Window>
typeWindowのフィールドを指定しHasDocumentます。
HasDocument hasDocument;
ウィンドウ コンストラクターはバインディングを作成する必要があります。
hasDocument = new HasDocument();
InitializeComponent();
Binding b = new Binding("Document");
b.Source = richTextBox;
b.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(hasDocument, HasDocument.DocumentProperty, b);
XAML でバインドを宣言できるようにする場合は、論理ツリーに挿入できるようにHasDocumentクラスを派生させる必要があります。FrameworkElement
ここで、 のDocumentプロパティを変更するHasDocumentと、リッチ テキスト ボックスDocumentも変更されます。
FlowDocument d = new FlowDocument();
Paragraph g = new Paragraph();
Run a = new Run();
a.Text = "I showed this using a binding";
g.Inlines.Add(a);
d.Blocks.Add(g);
hasDocument.Document = d;