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;
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>
How do I copy the contents of multiple selected items of a ListBox so that they fall in different lines in Excel and Notepad when pasted?
You can do this easily by extending the ApplicationCommands.Copy command as follows: See this forum for better formatting of the code ListView Copy to clipboard selected row. [XAML] <ListBox x:Name=’myListBox’ SelectionMode=’Multiple’> <ListBox.Items> <ListBoxItem>Item1</ListBoxItem> <ListBoxItem>Item2</ListBoxItem> <ListBoxItem>Item3</ListBoxItem> <ListBoxItem>Item4</ListBoxItem> </ListBox.Items> </ListBox> In your code behind: [C#] public Window1() { this.InitializeComponent(); CommandBinding cb = new CommandBinding(ApplicationCommands.Copy, CopyCmdExecuted, CopyCmdCanExecute); this.myListBox.CommandBindings.Add(cb); } void CopyCmdExecuted(object target, ExecutedRoutedEventArgs e) { ListBox lb = e.OriginalSource as ListBox; string copyContent = String.Empty; // The SelectedItems could be ListBoxItem instances or data bound objects depending on how you populate the ListBox. foreach (ListBoxItem item in lb.SelectedItems) { copyContent += item.Content.ToString(); // Add a NewLine for carriage return copyContent += Environment.NewLine; } Clipboard.SetText(copyContent); } void CopyCmdCanExecute(object sender, CanExecuteRoutedEventArgs e) { ListBox lb = e.OriginalSource as ListBox; // CanExecute only if there is one or more selected Item. if (lb.SelectedItems.Count > 0) e.CanExecute = true; else e.CanExecute = false; }
How do I make the UserControl B FILL the UserControl A it’s hosted in?
You can bind B’s Width and Height to that of A’s. For example, in the XAML where you define B: [XAML] <local:B Width='{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type A}}, Path=Width}’ /local:B> If there is an ActualWidth property in A, then you might want to bind B’s Width to that instead. Do the same for Height.