We present a new model for the X-ray properties of the intracluster medium that explicitly includes heating of the gas by the energy released during the evolution of cluster galaxies. We calculate the evolution of clusters by combining the semi-analytic model of galaxy formation of Cole et al. with a simple model for the radial profile of the intracluster gas. We focus on the cluster X-ray luminosity function and on the relation between X-ray temperature and luminosity. We show that cooling alone is not enough to account for the flatness of the observed T-L relation or for the lack of strong redshift evolution in the observed X-ray luminosity function. Gas heating, on the other hand, can solve these two problems: in the Lambda-CDM cosmology, our model reproduces fairly well the T-L relation and the X-ray luminosity function. Furthermore, it predicts only weak evolution in these two properties out to z=0.5, in agreement with recent observational data. A successful model requires an energy input of 1--2 x 10^49 ergs per solar mass of stars formed. This is comparable to the total energy released by the supernovae associated with the formation of the cluster galaxies. Thus, unless the transfer of supernovae energy to the intracluster gas is very (perhaps unrealistically) efficient, additional sources of energy, such as mechanical energy from AGN winds are required. However, the amplification of an initial energy input by the response of the intracluster medium to protocluster mergers might ease the energy requirements. Our model makes definite predictions for the X-ray properties of groups and clusters at high redshift which may soon be tested with data from the Chandra and Newton satellites.