Switch to fluid Switch to wfix Switch to fixed

StackPanel

dynamic rants = new Stack();

Archive for July, 2008

WPF Code FAQ

Posted by Vin on Jul-11-2008

I am a big believer in the power of Windows Presentation Foundation technology introduced by Microsoft in .Net framework 3.0. Subsequently since then in 3.5 and 3.5 beta 1 it’s more matured and gone much deeper and powerful with hardware acceleration and graphics capabilities. Microsoft Surface the table top multi touch interface computer/device can be a huge platform for WPF applications apart from desktop and enterprise apps.

We all know there are theoritical FAQs when we start learning and studying something. Today I found a very nice WPF coding FAQ which can be very useful. Infact it’s awesome.

Here’s a list of questions being answered.

1. Application Model

1.1 Is WPF resolution independent?
1.2 Should we manually cleanup the resources used by WPF bitmaps or Image controls?
1.3 Is it possible to change the Z-order of hosted Windows Forms control in WPF?
1.4 How to host a WPF/XBAP application in ASP.NET pages?
1.5 How to use Windows Forms control (e.g. Crystal Report Viewer) in a WPF application?
1.6 Is it possible to subclass from XAML generated class in WPF?

2. Base Services (Threading, Keyboard/Mouse handling & Dependency Properties)

2.1 Where is Application.DoEvents in WPF?
2.2 How to access WPF controls from another thread?

3. Controls

3.1 How to expand all nodes (TreeViewItems) of TreeView control?
3.2 How to do lasso (drag & select) selection in ListBox/ListView?
3.3 How to implement RadioButtonList type of control in WPF?
3.4 How to keep only one Expander control opened in a group of Expander controls?
3.5 How to automatically check TreeView children nodes when parent is checked?
3.6 What is ContentPresenter?

4. Text & Documents

4.1 How to find and replace text in RichTextBox?

5. Styles

5.1 When styling Button control, why does it have different theming behaviors under Windows XP and Windows Vista?
5.2 How to re-style or re-template a Popup control?
5.3 How to customize a Window in WPF?

6. Data Service

6.1 What’s the difference between TemplateBinding and Binding?
6.2 What is CollectionView?
6.3 How to enable multi-threaded data binding in WPF?

7. Visuals (2D/3D Graphics, Animation & Media)

7.1 How to use RenderTargetBitmap?
7.2 How to animate the size of a Window in WPF?
7.3 Why Viewport2DVisual3D.Camera is frozen when animating it?

8. Imaging

8.1 Is there any efficient method in WPF to draw graphics at pixel level?

Follow this link for the answers

Getting even with GridSplitter

Posted by Vin on Jul-9-2008

TreeView is not always the best way to represent complex hierarchical schematic data. Sometimes it becomes much more usable, just to use multiple ListBox/ListViews separated by Splitters that can be used to resize them. Just as an example, I’ve shown a fairly simple genre, album, artist scene here, but you might have multi-level structure and when you don’t want your users drilling down a long tree to figure out where their tiny little piece of data is hiding.

In WPF we have GridSplitter control placed in between the ListBox to do the job. You have like 3 ListBoxes to represent 3 levels of hierarchical data. Genre, Artist, Album let’s say. You thought that was easy. Wait until you run the app and start resizing. The resize wouldn’t evenly change the width of the adjacent ListBoxes. Either the Left ListBox would shrink in width and there would be a blank space in between or the right one would do the same. Why?

Answer:
Set the HorizontalAlignment=”Center” or HorizontalAlignment=”Stretch”, this property makes sure that it aligns itselfs horizontally as well not allowing space in between the splitter and the adjacent columns. Handy clue.

<GridSplitter Grid.Column="3" VerticalAlignment="Stretch"
Margin="0" Background="#999" ShowsPreview="True" Width="2"
HorizontalAlignment="Center"/>

BEFORE

Before Gridsplitter

AFTER

Gridsplitter


xmlns in Xml data islands

Posted by Vin on Jul-8-2008

You have an xml that you want to load up in a WPF treeview, use HierarchicalDataTemplate, the Xml coming from an Xml data island of a XmlDataProvider. But after hours of trying different XPath expressions, other samples and gravity defying stunts the Treeview doesn’t load up the Xml tree data. Why?

Answer: The root element of the Xml data island should have an xmlns attribute empty.


Note from MSDN

The root node of the XML data has an xmlns attribute that sets the XML namespace to an empty string. This is a requirement for applying XPath queries to a data island that is inline within the XAML page. In this inline case, the XAML, and thus the data island, inherits the System.Windows namespace. Because of this, you need to set the namespace blank to keep XPath queries from being qualified by the System.Windows namespace, which would misdirect the queries.

Example:


Here’s the msdn link(look under Note section)