I have a ListBox
which when clicked displays data to the user. If the user enters invalid data and they click another item in the list I want to prevent the ListBoxItem
they clicked from being selected. However, currently the ListBoxItem_Selected
event is firing after the ListBoxItem
is being selected, so I don't know how to stop the next list box item from being selected.
In the ListBoxItem_Selected
event, I'm validating the data but by this point it's too late.
Simply executing
((ListBoxItem)this.ListBox.Items[previousIndex]).IsSelected = true;
or
this.ListBox.SelectedIndex = previousIndex;
does not work, ie the next list item is still selected.
How can I prevent the next item from being selected if the data they entered is invalid?
So the best way to do this would be to Bind the Listbox enabled to a bool in your code.
Something like this
public bool enabledOrNot { get; set; }
<Listbox isEnabled={Binding enabledOrNot}>...
Then when you do the validation on your first input you can set this bool and it will enable or disable as needed.
Another way to do this would be to use ValidationRules. I know we use these in our code at work but they are to arcane for me to understand. All I know is they somehow validate before anything else takes place. Its a built in .NET feature
http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.