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; } } }
How do I hide the default context menu of the TextBox?
Simply set the ContextMenu property to null as follows: <TextBox ContextMenu='{x:Null}’>Testing</TextBox>
How come I don’t see a MouseHover event in WPF for it’s elements?
WPF doesn’t provide a MouseHover event and instead provides a much more flexible and high-level Tooltip support. To simulate MouseHover, you will first set the tooltip for the element in question to some dummy text and then listen to the TooltipOpening event where you would mark it as ‘Handled’ as follows: [XAML] <Canvas ToolTipOpening=’Canvas_ToolTipOpening’ ToolTipService.ToolTip=’Testing’> </Canvas> [C#] private void Canvas_ToolTipOpening(object sender, ToolTipEventArgs e) { e.Handled = true; // More code goes here: }