Preheat the oven to 375 degrees and lightly grease a 9x9 baking dish.
Melt the butter in a saucepan over medium heat. Whisk in the flour and cook for one minute.
Slowly whisk in the milk. Add the salt and pepper. Cook, stirring often, until the sauce thickens slightly.
Spread one third of the potato slices in the baking dish. Top with half the ham, half the onion, and one third of the cheese. Pour one third of the sauce over the layers.
Repeat the layers once more with another third of the potatoes, the remaining ham and onion, another third of the cheese, and another third of the sauce.
Finish with the last third of the potatoes, the remaining sauce, and the remaining cheese.
Cover the dish with foil and bake for 45 minutes. Remove the foil and bake 15 minutes longer until the top is golden and the potatoes are tender when pierced with a knife.