While it could be the solenoid at fault, the odds are very much stacked in favour of it being the brake microswitch assembly - especially as your brake lights are part of the problem. A common fault that is sometimes a failed microswitch - there are two of them - and sometimes a dry solder connection to the little circuit board. One switch (Normally Open) is for the brake lights and solenoid, the other (Normally closed) is to tell the ECU the brake state.
The brake switch assembly is quite easily removed but a potential nightmare to get back! The most useful way to show you what's involved is to put you through to this link where a huge variety of experience of this problem is shared.
https://www.jaguarforums.com/forum/xk8- ... ake+switch
If you need to get to solenoid, you'll need to remove the centre console which is not too difficult. There's videos on You Tube here
https://www.youtube.com/watch?v=kxbR7QsSjfw and here
https://www.youtube.com/watch?v=gU8QeZWoWr0