UWP XAML RadioButton IsChecked not working

Here is my XAML showing the first radio button being checked, yet when the form shows, none are selected.

When I click around on the form, the radio buttons behave as expected.

<StackPanel Margin="20,0" Orientation="Horizontal">
    <TextBlock Margin="0,0,10,0" VerticalAlignment="Center" Text="Search with:" />
    <RadioButton x:Name="rdoDates" Content="Dates" IsChecked="True" />
    <RadioButton x:Name="rdoTags" Content="Tags" IsChecked="False" />
    <RadioButton x:Name="rdoBoth" Content="Both" IsChecked="False" />
</StackPanel>

Update As I understand it, if your radiobuttons are inside a containing element, like a stackpanel or what ever you do not need a GroupName and adding IsChecked=True to one of them works, so I did not use a groupname. Everything works fine until you add a second group of radiobuttons later in the page flow that has one item IsChecked=True. The IsChecked property gets applied to the second group of radio buttons and the first one goes unchecked.

Once I applied a GroupName to each set of radiobuttons, the IsChecked was respected for each group.


I had same problem in UWP and using GroupName fixed it! I also used x:Bind to be able debug, but it didn't help.

<StackPanel Orientation="Vertical">
                    <RadioButton x:Uid="radioButtonOriginalMap"
                                 Tag="OriginalMap"
                                 GroupName="Map"
                                 IsChecked="{x:Bind Vm.IsRadarOriginalMapChecked, Mode=TwoWay}"/>
                    <RadioButton x:Uid="radioButtonBingMap"
                                 GroupName="Map"
                                 Tag="BingMap"
                                 IsChecked="{x:Bind Vm.IsRadarBingMapChecked, Mode=TwoWay}"/>
</StackPanel>

I faced similar problems as well. I don't exactly know why this happens. But to be sure that the radiobutton is checked every time, I did in the contructor of the page from the code behind.


Late to the party, but what helped me is to set unique x:Name and GroupName over the whole (!) xaml including all the controls in separate files. Maybe it is too strict, but I had two controls in different xamls which had identical Names inside, and once I modified one of them, the later loaded became OK as well.

HTH

链接地址: http://www.djcxy.com/p/44670.html

上一篇: 我如何绑定ControlTemplate中的RadioButton

下一篇: UWP XAML RadioButton IsChecked无法正常工作