Sunday, July 24, 2011

Extended WPF Toolkit Rocks

Recently while searching for wpf property grid I came across extended wpf toolkit by Brian Lagunas.
It not only contains an excellent wpf property grid but also a bunch of useful controls like
·         BusyIndicator
·         ButtonSpinner
·         Calculator
·         CalculatorUpDown
·         ChildWindow
·         ColorCanvas
·         ColorPicker  
·         DateTimePicker
·         DateTimeUpDown
·         DecimalUpDown  
·         DoubleUpDown  
·         IntegerUpDown  
·         Magnifier
·         MaskedTextBox
·         MessageBox
·         RichTextBox
·         RichTextBoxFormatBar
·         SplitButton  
·         TimePicker  
·         WatermarkTextBox
(What more u wants from god?J)
Though the documentation is self-explanatory I had made a small solution to demonstrate all the control in one window.

Tuesday, July 12, 2011

Understanding HierarchicalDataTemplate with TreeView

Right from the time I started making WPF UI HierarchicalDataTemplate are always mistery to me until I came across this article in Code Project

Though this article is excellent but it is focused on how to show checkbox with TreeView. In this blog I will explain the basic of the HierarchicalDataTemplate with the example shared in CodeProject article.
First thing you need to understand about HierarchicalDataTemplate (or HDT) is they are mean to show your hierarchical data, it can nested structures like
Class Foo
{
            String Name;
            List<Foo> Childs;
}
In above case your HDT should look like:

 <HierarchicalDataTemplate DataType="{x:Type base:Foo}" ItemsSource="{Binding Childs}">
                    <TextBlock Text="{Binding Name}"/>
 </HierarchicalDataTemplate>

As HDT is a data template you have to target a datatype in our case we have Foo class. Now ItemSource should be binded to the items which want to show. Please remember in this case as , all the Childs are again of type Foo so same HDT will be applied by the WPF parser to render it. Now as each Foo contains Name property you can bind it to any control defined in the template (in this case TextBlock).

You can show hierarchies of different classes using multiple classes using more than one HDT (do remember if your structure contains more list of more than one datatype you have to use more than one HDT). Like

Class Dummy
{
     List<string> names;
}

Class Foo
{
            String FooName;
            List< Dummy > Childs;
}
HDT will look like
 <HierarchicalDataTemplate DataType="{x:Type Foo}" ItemsSource="{Binding Childs}">
                    <TextBlock Text="{Binding FooName}"/>
                </HierarchicalDataTemplate>                
                <DataTemplate DataType="{x:Type Dummy}" >
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
 Note that here we had used another DataTemplate to further show each Dummy inside Foo.Childs.
You can also use more than one HDT when dealing with more complex data structure.
In the code (below link) there are 2 HDT used to show list of below class

public class Test
    {
        public string Name { get; set; }

        public List<FooViewModel> TestList { get; set; }
    }

Here FooViewModel is self-referential structure which contains both Parent & List of Childs of type FooViewModel.

<HierarchicalDataTemplate
                DataType="{x:Type local:FooViewModel}"
                    ItemsSource="{Binding Children}">
                    <StackPanel x:Name="SP" Background="{Binding BackgroundBrush}" Orientation="Horizontal">
                        <!-- These elements are bound to a FooViewModel object. -->
                        <CheckBox
                                    Focusable="False"
                                    IsChecked="{Binding IsChecked}"
                                    VerticalAlignment="Center"/>
                        <ContentPresenter
                                Content="{Binding Name}"
                                Margin="2,0"/>
                    </StackPanel>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate
                         DataType="{x:Type local:Test}"       
                        ItemsSource="{Binding TestList}">
                    <StackPanel Orientation="Horizontal">
                        <!-- These elements are bound to a FooViewModel object. -->
                        <ContentPresenter
                            Content="{Binding Name}"
                            Margin="2,0"/>
                    </StackPanel>
                </HierarchicalDataTemplate>

Also I had modified the example to show how to databing to change background colors (you check the checkbox ).

In most of the real life project you have to handle different behaviors of TreeView from ViewModel , like if you use search in treeview you have to hide/show items or you have to change the view based itemSelected in the treeview.

Please find the code at

I thing this article will be helpful to you. Please provide any feedback or post any questions.

Sunday, July 10, 2011

Accordion Control for WPF

Few days ago I was looking for the accordion control available in WPF for once of my requirement.
I have to show a Hierarchical object to the user, but the content of the accordion should change based on whether there is multiple object in second level or not. So if there are multiple objects in 2nd level I have to show it in Tab control while if there is single object I have to show it in Listview.

After some googling I figure out there are 3 possible options for that.
  1. Use toolkit Accordion Control.
  2. Make your own accordion.
  3. Use ComponentOne Accordion. 


Option 1 (ToolKit):
Pros:
  1.  Easy to use .
  2. Licensing cost.
  3. Can be configured to cater your need.
  4. You can data bind to the control.
Cons:
  1. Still in preview phase.
  2.  Lots of small defects (Like being a ItemControl it don’t support ItemTemplate). 


Option 2 (Custom Control):
You can use wpf treeview to behave like an accordion control.
Pros:
  1. Highly customizable to your needs.
  2.  Highly reliable unlike toolkit where u can find defect now & then.
  3. You can databind it.
  4. No Licensing cost.

Cons:
  1. You need to make your hands dirty (But that’s the best thing a developer wantsJ).
  2.  It’s difficult to match the flexibility of toolkit accordion (you can change the orientation etc.).


Option 3 (ComponentOne Accordion)
ComponentOne provide a wpf accordion control. But this control is made on top on WPF toolkit accordion so behaves like same except CopmponentOne provides support & a nice look to toolkit accordion.

Pros:
  1. A production ready control with nice template.
  2. Can be used with wpf databinding.
  3. Support from ComponentOne Team.

Cons:
  1.  Licensing Cost.
  2. As build on top of toolkit accordion so expect the same non uniform behavior (as I mentioned above no ItemTemplate support even it is ItemControl). 
(In my view why someone wants to pay for this control if it is made on top of toolkit accordionJ)

Complete solution is at 

It contains detail solution for all the 3 options above mentioned.

Please feel free to ask any question via comments. If you like this posting please follow my blog.