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();
Why do my SelectedValue bindings don’t work in the ComboBox?
One reason your binding to the SelectedValue may not work is if you have manually added ComboBoxItems as follows: [XAML] <ComboBox Name='mycombo' SelectedValue='{Binding Path=operator, Mode=TwoWay}' Width = '50'> <ComboBoxItem Content=''></ComboBoxItem> <ComboBoxItem>EQ</ComboBoxItem> <ComboBoxItem>NE</ComboBoxItem> <ComboBoxItem>CH</ComboBoxItem> <ComboBoxItem>GE</ComboBoxItem> <ComboBoxItem>LE</ComboBoxItem> </ComboBox> Instead, set the ItemsSource property to a list of strings as follows and the SelectedValue data binding will work fine: [XAML] <Window.Resources> <src:MyList x:Name='myList' x:Key='myList'></src:MyList> </Window.Resources> <ComboBox Name='mycombo' SelectedValue='{Binding Path=Code, Mode=TwoWay}' ItemsSource='{StaticResource myList}' Width = '50' Height='30' > Code Behind: [C#] public class MyList : List<string> { public MyList() { this.Add(''); this.Add('EQ'); this.Add('NE'); this.Add('CH'); this.Add('GE'); this.Add('LE'); } } </string>