SHANGHAI—U.S. cosmetics company Mary Kay Inc. is in talks to buy an office building in Shanghai as part of its expansion plans in China, bucking the trend in a country where rivals have eased back.