开发者

Select item with an unfocusable listbox

开发者 https://www.devze.com 2023-03-28 04:20 出处:网络
I\'m developing an application with wpf where a text box is always focused while all the other controls are never focused, to achieve this I\'ve set the property IsFocusable = false for all controls.

I'm developing an application with wpf where a text box is always focused while all the other controls are never focused, to achieve this I've set the property IsFocusable = false for all controls. One of this controls is a ListBox and if i set the IsFocusable = false property the ListBox items become unselectable but i need to be able to select this items.

The soltions that I've found are not very elegant and clean. One solution is to use an ItemControl with a styled RadioButton as items and then binding the RadioButton click event with some methods in my view model The other solution is to use an attached behavior to capture the mouse down event on the ListBox, then find which item is located at the mouse coordinates and then try to set this item as the selected item but if i simply set the SelectedItem property from the attached behavior nothings happen.

Is there a more elegant and efficient way to make the items of an unfocusable ListBox selectable?

Edit: Another possible solution is to register a class handler for the mouse left button donw event using the EventManager:

EventManager.RegisterClassHandler(typeof(ListBoxItem),
ListBoxItem.MouseLeftButtonDownEvent,
    new RoutedEventHandler((s, o) =>
        { (s as ListBoxItem).IsSelected = true; }));

Class Handlers are called before any other event handlers and for all controls of the specified type (ListBoxItem in this case) in the entire application

Th开发者_JAVA技巧is is the best solution I've found since now

0

精彩评论

暂无评论...
验证码 换一张
取 消