How do I specify a HierarchicalDataTemplate for a templated type?

Sometimes you might have to specify a HierarchicalDataTemplate for a templated type. If so, you can only do this in code-behind. (If you have to do this in XAML try wrapping that template type within a non-templated dummy type.) For example, if you have to do something like this: <HierarchicalDataTemplate ItemsSource='{Binding}’ DataType='{x:Type local:MyTemplatedList<int>}’> <TextBlock Text='{Binding Name}’></TextBlock> </HierarchicalDataTemplate> The above wouldn’t compile because the XAML compiler doesn’t support this. So, you will have to do this in code-behind instead: using MyListType = MyTemplatedList; // Then in Window’s constructor for example: HierarchicalDataTemplate hdt = new HierarchicalDataTemplate(typeof(MyListType)); hdt.ItemsSource = new Binding(); FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock)); tb.SetBinding(TextBlock.TextProperty, new Binding(‘Name’)); hdt.VisualTree = tb; this.Resources[new DataTemplateKey(typeof(CountryList))] = hdt; Note that the above code also illustrates how to add a HierarchicalDataTemplate resource to the Resources list without specifying a key.

How do I draw a border outside the bounds of a control?

WPF lets you specify a negative value for the Border’s Margin. This way the available size for the children of the Border will not reduce when a Border is rendered. <Border Margin=’-1,-1,-1,-1′ BorderThickness=’1′ BorderBrush=’Black’> <MyControls/> </Border>

How do I bind my XML Data to the tree (the xml contains multi-level hierarchical data with leaf nodes at many levels)?

Here is a sample XML and how it gets bound to a tree. The XML is of the form: CATEGORY ——REPORT —–CATEGORY ———-REPORT ——CATEGORY ———–CATEGORY —————–REPORT See how there can be any number of Category levels and the Reports can appear at any level too. Here is the sample code to bind this to the tree: (For better code formatting take a look at this incident in the MSDN Forums: A xml string…need to place nodes in a treeview. [XAML] <Window.Resources> <XmlDataProvider x:Key=’myReportsData’ XPath=’CATEGORY’ IsInitialLoadEnabled=’True’> <x:XData> <CATEGORY NAME=’main 1′ xmlns=”> <CATEGORY NAME=’sub main 1′> <REPORT NAME=’report111′> </REPORT> </CATEGORY> <CATEGORY NAME=’test2222′> <CATEGORY NAME=’test3333′> <REPORT NAME=’report_test222′> </REPORT> </CATEGORY> <CATEGORY NAME=’test555′> <REPORT NAME=’report_test333′> </REPORT> </CATEGORY> </CATEGORY> </CATEGORY> </x:XData> </XmlDataProvider> <HierarchicalDataTemplate DataType=’CATEGORY’ ItemsSource ='{Binding XPath=*}’> <TextBlock Text='{Binding XPath=@NAME}’ /> </HierarchicalDataTemplate> <DataTemplate DataType=’REPORT’> <TextBlock Text='{Binding XPath=@NAME}’ /> </DataTemplate> </Window.Resources> <TreeView Name=’myTreeView’ Background=’Aqua’ > <TreeView.ItemsSource> <Binding Source='{StaticResource myReportsData}’/> </TreeView.ItemsSource> </TreeView>

How do I bind a TreeView to a hierarchical list where some nodes have multiple child lists?

For example, if you have to bind to a hierarchical data source containing data like this: Companies Company Departments Department Employees Employee Computers Computer Where the Department type has 2 lists – Employees and Computers as follows: namespace HierarchicalData.Classes { public class Department { public string Name { get; set; } List _employees = new List(); public List Employees { … } List _computers = new List(); public List Computers { … } } } Then you will have to create templates as follows: <local:CompanyList x:Key=’companies’/> <HierarchicalDataTemplate x:Key=’EmployeeTemplate’> <TextBlock Text='{Binding Path=Name}’ /> </HierarchicalDataTemplate> <HierarchicalDataTemplate x:Key=’ComputerTemplate’> <TextBlock Text='{Binding Path=Name}’ /> </HierarchicalDataTemplate> <HierarchicalDataTemplate x:Key=’DepartmentTemplate’> <TreeViewItem Header='{Binding Path=Name}’> <TreeViewItem Header=’Employees’ ItemsSource='{Binding Path=Employees}’ ItemTemplate='{StaticResource EmployeeTemplate}’/> <TreeViewItem Header=’Computers’ ItemsSource='{Binding Path=Computers}’ ItemTemplate='{StaticResource ComputerTemplate}’/> </TreeViewItem> </HierarchicalDataTemplate> <HierarchicalDataTemplate x:Key=’CompanyTemplate’ ItemsSource='{Binding Path=Departments}’ ItemTemplate='{StaticResource DepartmentTemplate}’> <TextBlock Text='{Binding Path=Name}’ /> </HierarchicalDataTemplate> <TreeView Name=’_myTreeView’ Margin=’0,0,0,0′ ItemsSource='{Binding Source={StaticResource companies}}’ ItemTemplate='{StaticResource CompanyTemplate}’/>

How do I bind the values of an enum to a ComboBox ?

ComboBox can be bound to an enum in XAML or in code. The below code shows binding to the ‘Visibility’ enum, [XAML] <ObjectDataProvider x:Key=’VisibilityList’ ObjectType='{x:Type sys:Enum}’ MethodName=’GetValues’> <ObjectDataProvider.MethodParameters> <x:TypeExtension TypeName=’sys:Visibility’> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <ComboBox x:Name=’ComboBox1′ ItemsSource='{Binding Source={StaticResource VisibilityList}}’ SelectedIndex=’0′ /> Same thing in code-behind: [C#] // Setup the binding as follows: comboBox1.ItemsSource = Enum.GetValues(typeof Visibility); You can retrieve the selected enum value at any time using a simple cast as follows: [C#] Visibility visibility = (Visibility)this.myCombo.SelectedItem;