Gravitational binding energy of a galaxy is simply the energy needed to move each particle (star) in it to infinity - is equivalent of escape velocity.
For each small element of radius simply find the mass inside that radius, then the gravitational potential energy of a star at that radius is just = G m_star m_galaxy /R
You have to then recalculate the remaining mass of the galaxy at each step