In a tight primary for governor in 2018, Tim Walz, then a congressman from a conservative pro-gun district in Minnesota, was rebuked for his A ratings from the National Rifle Association. After a school shooting in Parkland, Fla., he went on the offensive.

Mr. Walz wrote an opinion piece asserting that he had “repeatedly voted in favor” of tougher background checks, federal gun violence research and firearm bans for people on no-fly lists. He had “voted for universal background checks more than anybody in this race,” he told an interviewer. And he posted a video in which he said he had voted “dozens of times” in Congress for stronger gun laws.

Gun advocates, however, had never questioned his loyalty as a legislator. The N.R.A. kept giving him high marks until he ran for governor, and Guns & Ammo magazine in 2016 named him one of its top 20 lawmakers.

“While most congressional Democrats have jumped on the gun control train with both feet,” the magazine said, “Tim Walz and a few others have stuck to their guns.”

Which version of Mr. Walz, now running for vice president, was right?

The answer lies in his bumpy transition from an unabashed small-town gun guy to a statewide candidate facing an electorate with starkly different views. It was a change propelled in part by high-profile mass shootings that also became a campaign issue.