Getting an Item Template to raise selected event properly
-
Here is my setup: I have a ListBox which contains an ItemTemplate. The ItemTemplate incorporates a checkbox with name/checked state bound to the ItemSource object. issue: when the checkbox is checked (or unchecked) the selected event is not picked up. Basically you have to click the checkbox text on the far right to actually select it. This can be a rather delecate issue as a user may think they are setting values for the item they checked but actually changing values for a previously selected item. Is there something in the xaml I can set or do I handle the checked event and set that object to selected item?
-
Here is my setup: I have a ListBox which contains an ItemTemplate. The ItemTemplate incorporates a checkbox with name/checked state bound to the ItemSource object. issue: when the checkbox is checked (or unchecked) the selected event is not picked up. Basically you have to click the checkbox text on the far right to actually select it. This can be a rather delecate issue as a user may think they are setting values for the item they checked but actually changing values for a previously selected item. Is there something in the xaml I can set or do I handle the checked event and set that object to selected item?
If I'm reading correctly - the checkbox button is eating the mouse click events (like buttons do) so the listbox selection doesn't change, correct? If so, I would try adding Checked/Unchecked event handlers to the CheckBox. In the handlers, the sender parameter will be the CheckBox. The CheckBox's DataContext should be a reference to the selected item in the ListBox's ItemsSource. Set the listBox's SelectedItem property to the CheckBox's DataContext value. (Note I didn't test this :))
private void CheckBox\_Checked(object sender, RoutedEventArgs e) { myListBox.SelectedItem = (sender as CheckBox).DataContext; } private void CheckBox\_Unchecked(object sender, RoutedEventArgs e) { myListBox.SelectedItem = (sender as CheckBox).DataContext; }
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
If I'm reading correctly - the checkbox button is eating the mouse click events (like buttons do) so the listbox selection doesn't change, correct? If so, I would try adding Checked/Unchecked event handlers to the CheckBox. In the handlers, the sender parameter will be the CheckBox. The CheckBox's DataContext should be a reference to the selected item in the ListBox's ItemsSource. Set the listBox's SelectedItem property to the CheckBox's DataContext value. (Note I didn't test this :))
private void CheckBox\_Checked(object sender, RoutedEventArgs e) { myListBox.SelectedItem = (sender as CheckBox).DataContext; } private void CheckBox\_Unchecked(object sender, RoutedEventArgs e) { myListBox.SelectedItem = (sender as CheckBox).DataContext; }
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark, Yup, you understood correctly, the checkbox is gobbling up the event and the listbox does not select the item. I pretty much thought I'd have to do what you suggested, setting listbox.SelectedItem to the corresponding checkbox. Thanks Mark, Michael
-
Mark, Yup, you understood correctly, the checkbox is gobbling up the event and the listbox does not select the item. I pretty much thought I'd have to do what you suggested, setting listbox.SelectedItem to the corresponding checkbox. Thanks Mark, Michael
I added a code sample to my post....does it work?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I added a code sample to my post....does it work?
Mark Salsbery Microsoft MVP - Visual C++ :java:
yuppers, that worked. :cool:
-
yuppers, that worked. :cool:
Good to know, thanks!
Mark Salsbery Microsoft MVP - Visual C++ :java: