hanuman523

I know you can programatically prevent user input in a textbox by hooking the KeyDown event and setting e.handled to true. Is there a way to do this with a listbox Depending on the state of the page I want to prevent the listbox from having the selectedindex changed. This would apply to both mouse and keyboard input.


Re: Windows Presentation Foundation (WPF) preventing listbox selection change

lee d

try setting IsHitTestVisible="false"




Re: Windows Presentation Foundation (WPF) preventing listbox selection change

Josh Smith

There is no event which fires before the selected index changes, so you cannot cancel a selection. The WPF team caught some heat for this a while back (old threads on this forum discuss the issue). What you can do is handle the SelectionChanged event and, if the selected index is not the desired selected index, set the SelectedIndex property to the correct value. Hacky, no doubt, but it should do the trick for you.




Re: Windows Presentation Foundation (WPF) preventing listbox selection change

Josh Smith

lee,

Using IsHitTestVisible won't prevent keyboard input from selecting items.






Re: Windows Presentation Foundation (WPF) preventing listbox selection change

lee d

yep, you are right




Re: Windows Presentation Foundation (WPF) preventing listbox selection change

hanuman523

The thing is, I want to be able to do some processing first. I want the user to be able to click on another item in the listbox and then, depending on whether certain criteria are met in the app, allow or deny the selection. Was hoping there was an event somewhere in the chain that could be manually handled. I could set IsHitTestVisible inside an event and achieve this but now I can find an event that fires before the selection is actually changed.




Re: Windows Presentation Foundation (WPF) preventing listbox selection change

hanuman523

Ahhh, thanks for the help guys!

BTW, I did search around the forum for this before posting but didn't find anything. Was probably being to specific in my search.




Re: Windows Presentation Foundation (WPF) preventing listbox selection change

Josh Smith

No problem, I'm glad to help out. Please mark the reply(s) which answered your question as The Answer so that others will know this thread contains useful information. Thanks!