Been messing with
WPF, usually when a new language comes out to get familiar with it I write a simple test applicaiton. Normally this is a replica of Notepad, I usually write an exact replica of Notepad with every single feature duplicated. This tends to get me quit familiar with the new technology, from there I can move on to more interesting things. Well with WPF this has taken me far longer than any language I have done this in thusfar, it has so many unique things in it, that are completely foreign to me and most developers.
So one thing I noticed that was quit unusual was how the
RichTextBox worked, it is much much more powerful to a point where it's nearly unbelievable to the point it can be extended, I expect to see alot of new programs with a very unique text support interface coming out with this thing. But more to the point, loading a text file into a RichTextBox in WPF is less than straight forward.
Below is how you do it:
using (Stream stream = fileDialog.OpenFile())
{
TextRange tr = new TextRange(rtMain.Document.ContentStart, rtMain.Document.ContentEnd);
tr.Load(stream, DataFormats.Text);
}
Now my first impressions were what the hell? How does it work? So on further investigation it's quit unique, the ContentStart, and ContendEnd properties are actually something called TextPointer and are indeed references and not integer values as one would expect from previous versions. While this is not apparent it is much more elegant and gives you much more flexability at adding text in any spot you wish without changing the caret position, etc.
So basically TextRange holds these two pointers, and when you load the file from a stream it takes the contents of the stream and dumps that in between those two pointers. So there you go, now it makes sense!