Today I was playing around with the RichTextBox and I found out something the hard way…. There is no Text property. I started googling about this and I found out that instead of a Text property there is a Document property that returns a FlowDocument. This is really cool because it means that we can have all the goodies of a flow document in our RichTextBox but the big question for me was still how do I get what the user entered in a simple string??
Well the answer lies in 1 line of code
So what we are doing here is just reading the FlowDocument content and getting a string out of it.
This is all cool but then I needed something else. I wanted to bind the RichTextBox to a String property in an object. First I tried with a converter, but guess what the Document property of the RichTextBox is not a Dependency object thus I could not bind and do a converter that returns a FlowDocument.
No worries…. With WPF there is always a way out
I created an Attached property called Text that will add a string in the RichTextBox default FlowDocument. Here is how I did it
The behavior I chose was to clear any other text from the TextBox and replace it with the string that is supplied. Obviously this is really easy to change, you just have to change the textBox.Document.Blocks.Clear()
RichTextBox is really an impressive control… I fell in love with it (and yea let’s not forget SpellCheck.IsEnabled="True" which is one of my new favorite goodies ) so don’t let something as simple as the Text property keep you away from using such a powerful control
Impressive things are coming out with WPF 4
Loads of new stuff that will make our lives much easier. Goodies such as
Win 7 taskbar
Win 7 ribbon
Focus management improvement
Support for UIAccessible2
Full Trust XBaps
Media element improvements
A lot of bug fixes
All new stuff from .Net 4.0
Let’s start from here… The AvalonControlsLibrary is just a Listview (with a GridView as view) that creates it’s GridViewColumns dynamically on runtime by looking at the properties(and their metadata) of the bound object. So how can you control the look of the columns if they are being generated dynamically? Well here we can use the power of WPF’s styling system. We can create a Style in a ResourceDictionary that does not have the x:Key set but only the TargetType. By doing so we will be setting the Style to all our GridViewColumns.
An example for this is setting a Style for the Headers. Here is the XAML to do so
Hope this helps
P.S For anyone using the DataGridView I would suggest to have a look at the new DataGrid control from Microsoft. It’s really really COOL!