How do I listen to click events on all the MenuItems through the parent ContextMenu?
Instead of setting up event handlers on all the MenuItems you can listen to those events as they bubble up to the context menu as follows: [XAML] MenuItem1 MenuItem2 MenuItem3 MenuItem4 Or you can setup the same thing in code as follows: [C#] menuItem1.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MnItem_Click)); The event handler would then be something like: private void MnItem_Click(object sender, RoutedEventArgs e) { MenuItem item = e.Source as MenuItem; …. }
How do I listen to click events on all the tree nodes through the parent tree?
Instead of setting up event handlers on all the tree nodes you can listen to those events as they bubble up to the tree as follows: [XAML]<TreeView TreeViewItem.Selected=”TvItem_Selected”> <TreeViewItem>Node1</TreeViewItem> <TreeViewItem>Node2</TreeViewItem> <TreeViewItem>Node3</TreeViewItem> <TreeViewItem>Node4</TreeViewItem></TreeView> Or you can setup the same thing in code as follows: [C#] treeView1.AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(TvItem_Selected)); The event handler would then be something like: private void TvItem_Selected(object sender, RoutedEventArgs e) { TreeViewItem item = e.Source as TreeViewItem; …. }
How do I change the background of the selected item in the ListBox?
You can change the colors in your ListBox’s resources, as follows: [XAML] <ListBox> <ListBox.Resources> <SolidColorBrush x:Key='{x:Static SystemColors.HighlightBrushKey}’ Color=’White’/> <SolidColorBrush x:Key='{x:Static SystemColors.HighlightTextBrushKey}’ Color=’Black’/> <SolidColorBrush x:Key='{x:Static SystemColors.ControlBrushKey}’ Color=’White’/> </ListBox.Resources> </ListBox> The 3rd color above is the gray color in which the selected item background is rendered when the listbox does not have the focus.
How do I bind a TextBlock to a resource string defined in the application’s resource1.resx file?
If the resource string is named ‘CopyrightInfo’ for example, then you can bind it to the TextBlock as follows: <TextBlock Text='{x:Static local:Resource1.CopyrightInfo}’/> Where the ‘local’ should be mapped to Resource1.resx’s namespace (you can find out by opening the resx’s code-behind file).
How do I apply custom sorting to the order of the items in a bound listbox?
To specify custom sorting using a custom implementation of an IComparer, you will have to use a CollectionViewSource, get it’s default view and set it’s CustomSort property as follows: Sample ListBox bound to a CollectionViewSource: <Window x:Class=’WindowsApplication1.Window1′ xmlns=’http://schemas.microsoft.com/winfx/2006/xaml/presentation’ xmlns:x=’http://schemas.microsoft.com/winfx/2006/xaml’ xmlns:scm=’clr-namespace:System.ComponentModel;assembly=windowsbase’ > <Window.Resources> <XmlDataProvider x:Key=’list’ > <x:XData> <Items xmlns=”> <Item> <Name>David</Name> <Age>20</Age> </Item> <Item> <Name>Marcus</Name> <Age>25</Age> </Item> <Item> <Name>George</Name> <Age>25</Age> </Item> <Item> <Name><![CDATA[Peter&#M]]></Name> <Age>25</Age> </Item> </Items> </x:XData> </XmlDataProvider> <CollectionViewSource Source='{Binding Source={StaticResource list}, XPath=Items/Item/Name}’ x:Key=’data’/> </Window.Resources> <ListBox Name=’lb1′ DisplayMemberPath=’Name’ ItemsSource='{Binding Source={StaticResource data}}’/> </Window> Then in code-behind specify the custom comparer as follows: public partial class Window1 : Window { public Window1() { InitializeComponent(); this.lb1.Loaded += delegate { CollectionViewSource cvs = this.TryFindResource(‘data’) as CollectionViewSource; if (cvs != null) { ListCollectionView view = cvs.View as ListCollectionView; if (view != null) { view.CustomSort = new XmlComparer(); } } }; } public class XmlComparer : IComparer { public Int32 Compare(Object x, Object y) { XmlElement a = x as XmlElement; XmlElement b = y as XmlElement; if (a != null && b != null) { return String.Compare(a.InnerText, b.InnerText); } return -1; } } }