Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area

前端 未结 4 905
广开言路
广开言路 2020-12-18 11:26

I\'ve a data template for ListBoxItem which contains of few buttons, and few custom controls like Grid or Chart. Each button is bound to an appropriate command handler, Sel

4条回答
  •  渐次进展
    2020-12-18 11:56

    Add this to your ListBox.Resources

    
        
    
    

    EDIT

    The previous method only makes the ListBoxItem selected for as long as it has keyboard focus. If you move focus out of the ListBoxItem, it becomes unselected again.

    Here's another simple way to select a ListBox item when the keyboard focus moves within the item, and it stays selected when focus is moved out of the ListBoxItem

    
    

    And in the Code Behind

    protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
    {
        ListBoxItem item = (ListBoxItem)sender;
        item.IsSelected = true;
    }
    

提交回复
热议问题