I am using a quite simple Tree Structure:
class Tree { }
class Node : Tree {
public List <Tree> Children;
}
class Leave : Tree {
public string Content;
}
And I want to display it in a WPF – RichTextBox, the Result look something like that, but bound to my Tree:
<FlowDocument>
<Section>
<Paragraph>
TEXT_FOOTER
</Paragraph>
</Section>
</FlowDocument>
Where only the leaves of the tree display their Content in a Paragraph, and Nodes display in a Section.
Currently I am solving this Programmatically, but to keep the RTB in Sync with my Tree is a Hard piece of work.
Is there way to bind such a structure to the Box, I know that the Document-Property is not a Dependency Property, so no binding.
I came over this TextBox: Here, But i am not shure if and how I can bind it, wpf does not seem to Know a HierachicalDataTemplate at this point.
Is there any way to make wpf take my synchronisation work?
I finally kind of solved it. The Problem is: WPF does not give me ANY possibilities to use DataBinding, so I wrote around using the “onTextElementChanged” and text.Selection.Start.Parent to recieve the Run the cursor was in.
My corresponding Datastructure i wanted to Map was stored in the “Tag”-Property of the parent Paragraph. I know its quite a ugly solution, but it was the only one that worked Properly.
If something changes, I had to concatinate multiple Runs, due the RTB splits them at random to write the changes back. Thats why I took the Paragraph to store it.
The other direction could be solved by listening to the PropertyChanged events, just like the WPF presents it to you.
The biggest an still unsolvable issue is the undo functionality of the RTB. Because I had to build a more complex Data structure, I used some UserControls and InlineUIContainer. If you remove one of them (for example by pressing “entf”) the programm crashes with a StackOverflowException. It is a “undocumented Feature” or bug in the WPF, and Microsoft does not bother to fix it.