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 { public MyList() { this.Add(”); this.Add(‘EQ’); this.Add(‘NE’); this.Add(‘CH’); this.Add(‘GE’); this.Add(‘LE’); } }
How do I add an item to the combo’s choice list with value empty string in XAML?
Your first instinct is probably to add a ComboBoxItem as follows: [XAML] <ComboBoxItem></ComboBoxItem> But, that wouldn’t work. Instead you will have to specify an empty string as follows: [XAML] <ComboBoxItem Content=”></ComboBoxItem>
How do I bind the ListBox to a collection Property in my Window?
With a listbox like this defined in your XAML: [XAML] <ListBox x:Name=’myLB’ ></ListBox> You can do so in your code-behind as follows: [C#] public MyWindow() { InitializeComponent(); Binding binding = new Binding(); binding.Source = this; PropertyPath path = new PropertyPath(‘ListSource’); binding.Path = path; // Setup binding: BindingOperations.SetBinding(this.myLB, ListBox.ItemsSourceProperty, binding); } public string[] ListSource { get { return new string[] { ‘testing’, ‘test1’ };} }
How do I capture Ctrl + Enter key press in my TextBox?
You can do so by listening to the ComponentDispatcher.ThreadPreprocessMessage in your window as follows: [C#] using System; using System.Windows; using System.Windows.Controls; using swf = System.Windows.Forms; using System.Windows.Interop; using System.Windows.Forms.Integration; using System.Windows.Input; namespace WpfTestHarness { public partial class CustomShortcutHandlingDemo : Window { public CustomShortcutHandlingDemo() { InitializeComponent(); // Registering against a stack event will cause memory leak, please unregister this event when you are done with it. ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage; } const int WM_KEYDOWN = 0x100; const int WM_SYSKEYDOWN = 0x0104; private void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled) { if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN) { // Examine if the Control and Enter keys are pressed, we also need to make sure that the // currently keyborad focused element is TextBox instance itself. swf.Keys keyData = ((swf.Keys)((int)((long)msg.wParam))) | swf.Control.ModifierKeys; if (((keyData & swf.Keys.Control) == swf.Keys.Control) && ((keyData & swf.Keys.Enter) == swf.Keys.Enter) && Keyboard.FocusedElement == textBox) { MessageBox.Show(‘Ctrl+Enter is pressed’); } } } } }
How do I change the background of my Grid layout when one of it’s children gets keyboard focus?
You can change the background of the grid layout when one of its children gets keyboard focus by setting the IsKeyboardFocusWithin property. This can be done with the following code snippet, [XAML] <Window.Resources> <Style x:Key = “GridTriggerStyle” TargetType = “Grid”> <Style.Triggers> <Trigger Property = “IsKeyboardFocusWithin” Value = “True”> <Setter Property = “Background” Value = “Green” /> </Trigger> <Trigger Property = “IsKeyboardFocusWithin” Value = “False”> <Setter Property = “Background” Value = “AliceBlue” /> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid Name=”testGrid” Style=”{StaticResource GridTriggerStyle}”> <Button Height=”30″ Width=”100″ Content=”SampleButton1″/> </Grid>