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<employee> _employees = new List<employee>(); public List<employee> Employees { ... } List<computer> _computers = new List<computer>(); public List<computer> Computers { ... } } } </computer></computer></computer></employee></employee></employee> 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;
How do I bind the ContextMenu to a list of commands?
It’s possible that you have a list of commands that you simply want to bind to a context menu. You can do so as follows. For example, if you have list of custom commands, each of this type: [C#] public class MyCommand { public Name{get; set;} public Command{get; set;} } Then you can bind a list containing the above items to a ContextMenu as follows: <ContextMenu> <ContextMenu.ItemContainerStyle> <Style TargetType='{x:Type MenuItem}'> <Setter Property='Command' Value='{Binding ContextMenuItemCommand}'/> </Style> </ContextMenu.ItemContainerStyle> </ContextMenu> <ContextMenu ItemsSource={StaticResource myList}></ContextMenu>