Go to a park. Tell DD that as long as she keeps her hat on you will stay, but if she takes if off you go home. AS SOON AS IT COMES OFF HER HEAD, you head straight home. Absolutely no "put it on or we're leaving"! This is a one strike and you're out lesson. Try it again a day or two later, and remind her of the rules. I promise after just a couple times having her fun cut short, she will stop playing games with the hat. My kids know that hats are not an option, even if they are only playing in the backyard. It's second nature and I don't regret the effort it took to get it that way.
BTW, I would also recommend a strap if only to make sure it stays put while DS is having fun.