InplaceSearchEnabled doesn't work when set programmatically in XsltListViewWebPart

There are a lot of forum threads about the error that InplaceSearchEnabled property of XsltListViewWebPart (XLV) cannot be changed programmatically, only through UI and WebPart properties.
I experienced the same issue when was trying to add XLV to sub site (display items of root level list on subsite) - the search input was not displayed and the InplaceSearchEnabled property was set to null.
Finally I found the solution (actually a workaround) and it is described below.