Tuesday, November 24, 2009

Useful code snippets for WPF developer.

Download snippets - here

I want to share very useful in my opinion code snippets for WPF developers. Most of they were created my friend - Dmitrij Zakharov. Moreover I want to add a short description to each snippets.

Note:
The Code Snippet Inserter inserts a code snippet at the cursor location, or inserts a surround-with code snippet around the currently selected code.

Don't you know how to use code snippets? You should read next article - How to: Use Code Snippets (C#). You should copy the code snippets files to Installation directory\Microsoft Visual Studio 9.0\VC#\Snippets\1033\NetFX30 in your PC for start to use.

Let me start:
  • addowner - Adds owner to the dependency property that already exists.
  • defstyle - Code snippet for overriding DefaultStyleKey.
  • propa - Code snippet for an attached property using DependencyProperty as the backing store. It is the common code snippet that is delivered with installation of VS2008
  • propdp - Code snippet for a property using DependencyProperty as the backing store. It is the common code snippet that is delivered with installation of VS2008
  • propdpec - Code snippet for a property with value caching and events support using DependencyProperty as the backing store.
  • propdpr - Code snippet for a property using DependencyPropertyKey as the backing store.
  • routedcmd - Implements routed command executor.
  • routedevent - Declares and implements routed event.
PS: Do you want to know more? - read about Default Code Snippets.

Thursday, November 19, 2009

Zoom and page changed events for FlowDocumentPageViewer

Download source files - here

My old known Bala send to me this link.
Kent Boogaart has written an article that describes approach searching and highlighting text in FlowDocumentPageViewer - Search and Highlight Text in an Arbitrary Visual Tree. Unfortunate, two serious issues present there. I copy theirs description from msdn forum:
  1. When I change the pages of the FlowDocumentPageViewer, my Rectangular highlighted area remains the same and it is not sinking with the Text.
  2. When I zoom in or zoom out of the FlowDocumentPageViewer, the text gets zoomed but the Highlight rectangle remains in the same position.
Seems.. solution should be easy - just add DoSeach() method in PageChanged and ZoomChanged event handler methods of FlowDocumentPageViewer but it doesn't have events for notice these actions. So I decided just to implement these events :). FlowDocumentPageViewer doesn't have protected and virtual methods like OnZoomChanged or OnPageChanged. It was bad. But I found four protected and virtual methods that could help me they are OnNextPageCommand(), OnPreviousPageCommand(), OnIncreaseZoomCommand() and OnDecreaseZoomCommand(). When I added call to rise my events in theirs override methods I found that this approach wasn't work. It happened because command logic works through concurrency approach. I called the rise methods through BeginInvoke for delay the calls rise methods - and problem was solved.
You can download sources with fix above.

PS: I didn't have time for full investigate sources.. but one question. Why did Kent use SearchRectangle class? In my opinion common Rect is enough.

Monday, November 16, 2009

How to implement TextBox with PlaceHolder in WPF

Download source files - here

Using PlaceHolder is very useful thing for UI in any type application. I want to show a short way how can you implement it in WPF.
You can custom TextBox and add place-holder text on MouseEnter and remove on MouseLeave events. In this case place-holder's text looks like original text.. You can experiment with Foreground property, but truth me - you will not be satisfy.
Let my suggest other way. You can use AdornerLayer for host the place-holder text. In this case you should create class that is inherit from Adorner and this class should show your place-holder text on AdornerLayer. I've create little sample that demonstrate this.

Unfortunate, AdornerLayer doesn't presented over any Popup controls. So this solution isn't worked if you put our customized TextBox control in any Popup and as item in Menu or ComboBox controls.