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.

How do I show a MessaegBox after my main Window and it’s contents are all loaded and visible?

Use the Dispatcher object to make your method run after all the ‘background’ operations are completed, as follows: public Window1() { InitializeComponent(); this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new SomeMethodDelegate(DoSomething)); } private delegate void SomeMethodDelegate(); private void DoSomething() { MessageBox.Show(‘Everything is loaded.’); }

How do I move the focus to a ListViewItem that could also be out of view?

You can do so as follows: [C#] this.myList.SelectedItem = o; // In case the item is out of view. If so, the next line could cause an exception without bringing this item to view. myList.ScrollIntoView(this.myList.SelectedItem); ListViewItem lvi = (ListViewItem)myList.ItemContainerGenerator.ContainerFromIndex(myList.SelectedIndex); lvi.Focus();