c# - Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area -
c# - Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area -
i've info template listboxitem contains of few buttons, , few custom controls grid or chart. each button bound appropriate command handler, selectedindex property of listview command bound viewmodel's propery well.
the problem: in command handlers (which bound buttons) can't resolve selected item/index because not changing whilst clicking on button or other command within listbox item, when clicking on listboxitem area - selectedindex changing.
question is how trigger selectedindex changed whilst clicking on command within listboxitem?
add listbox.resources
<listbox.resources> <style targettype="{x:type listboxitem}"> <style.triggers> <trigger property="iskeyboardfocuswithin" value="true"> <setter property="isselected" value="true" /> </trigger> </style.triggers> </style> </listbox.resources> edit
the previous method makes listboxitem selected long has keyboard focus. if move focus out of listboxitem, becomes unselected again.
here's simple way select listbox item when keyboard focus moves within item, , stays selected when focus moved out of listboxitem
<style targettype="{x:type listboxitem}"> <eventsetter event="previewgotkeyboardfocus" handler="selectcurrentitem"/> </style> and in code behind
protected void selectcurrentitem(object sender, keyboardfocuschangedeventargs e) { listboxitem item = (listboxitem)sender; item.isselected = true; } c# .net wpf listview
Comments
Post a Comment