Maui多个RadiobuttonGroup不起作用。

huangapple go评论87阅读模式
英文:

Maui multiple RadiobuttonGroup not working

问题

我有一个带有两个单选按钮组的测试项目。页面上的第一组不会在页面加载时显示应该选择的单选按钮。下面的示例项目演示了这个问题。

表单有两行单选按钮:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="RadioButtonGroupTest.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="0"
            Padding="30,30,0,0"
            VerticalOptions="Start">

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition
                        Height="*"></RowDefinition>
                    <RowDefinition
                        Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                
                <Grid.ColumnDefinitions>
                    <ColumnDefinition
                        Width="1*"></ColumnDefinition>
                    <ColumnDefinition
                        Width="5*"></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Label
                    Text="Group 1"
                    Grid.Row="0"
                    Grid.Column="0"></Label>

                <HorizontalStackLayout
                    Grid.Row="0"
                    Grid.Column="1"
                    RadioButtonGroup.GroupName="Group1">

                    <RadioButton
                        x:Name="Group1YesRadioButton"
                        Content="Yes"
                        IsChecked="{Binding Path=Group1YesChecked}"
                        Value="Yes">
                    </RadioButton>

                    <RadioButton
                        x:Name="Group1NoRadioButton"
                        Content="No"
                        IsChecked="{Binding Path=Group1NoChecked}"
                        Value="No">
                    </RadioButton>
                </HorizontalStackLayout>

                <Label
                    Text="Group 2"
                    Grid.Row="1"
                    Grid.Column="0"></Label>

                <HorizontalStackLayout
                    Grid.Row="1"
                    Grid.Column="1"
                    RadioButtonGroup.GroupName="Group2">

                    <RadioButton
                        x:Name="Group2RedRadioButton"
                        Content="Red"
                        IsChecked="{Binding Path=Group2RedChecked}"
                        Value="Red">
                    </RadioButton>

                    <RadioButton
                        x:Name="Group2GreenRadioButton"
                        Content="Green"
                        IsChecked="{Binding Path=Group2GreenChecked}"
                        Value="Green">
                    </RadioButton>

                    <RadioButton
                        x:Name="Group2BlueRadioButton"
                        Content="Blue"
                        IsChecked="{Binding Path=Group2BlueChecked}"
                        Value="Bule">
                    </RadioButton>
                </HorizontalStackLayout>
            </Grid>
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>

视图模型有5个布尔值:

public class ViewModel : INotifyPropertyChanged
{
    public bool Group1YesChecked { get; set; }
    public bool Group1NoChecked { get; set; }
    public bool Group2RedChecked { get; set; }
    public bool Group2GreenChecked { get; set; }
    public bool Group2BlueChecked { get; set; }

    public ViewModel() 
    { 
        Group1YesChecked = true;
        Group1NoChecked = false;
        Group2RedChecked = false;
        Group2GreenChecked = true;
        Group2BlueChecked = false;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

页面的代码设置了视图模型:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        ViewModel vm = new ViewModel();
        this.BindingContext = vm;
    }
}

应用程序运行时主页面显示

我期望"Yes"按钮被选中。

英文:

I have a test project with two radio button groups. The first group on the page will not show the radio button that should be selected when the page loads. The sample project below demonstrates the problem.

The form has two rows of radio buttons:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
x:Class=&quot;RadioButtonGroupTest.MainPage&quot;&gt;
&lt;ScrollView&gt;
&lt;VerticalStackLayout
Spacing=&quot;0&quot;
Padding=&quot;30,30,0,0&quot;
VerticalOptions=&quot;Start&quot;&gt;
&lt;Grid&gt;
&lt;Grid.RowDefinitions&gt;
&lt;RowDefinition
Height=&quot;*&quot;&gt;&lt;/RowDefinition&gt;
&lt;RowDefinition
Height=&quot;*&quot;&gt;&lt;/RowDefinition&gt;
&lt;/Grid.RowDefinitions&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition
Width=&quot;1*&quot;&gt;&lt;/ColumnDefinition&gt;
&lt;ColumnDefinition
Width=&quot;5*&quot;&gt;&lt;/ColumnDefinition&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Label
Text=&quot;Group 1&quot;
Grid.Row=&quot;0&quot;
Grid.Column=&quot;0&quot;&gt;&lt;/Label&gt;
&lt;HorizontalStackLayout
Grid.Row=&quot;0&quot;
Grid.Column=&quot;1&quot;
RadioButtonGroup.GroupName=&quot;Group1&quot;&gt;
&lt;RadioButton
x:Name=&quot;Group1YesRadioButton&quot;
Content=&quot;Yes&quot;
IsChecked=&quot;{Binding Path=Group1YesChecked}&quot;
Value=&quot;Yes&quot;&gt;
&lt;/RadioButton&gt;
&lt;RadioButton
x:Name=&quot;Group1NoRadioButton&quot;
Content=&quot;No&quot;
IsChecked=&quot;{Binding Path=Group1NoChecked}&quot;
Value=&quot;No&quot;&gt;
&lt;/RadioButton&gt;
&lt;/HorizontalStackLayout&gt;
&lt;Label
Text=&quot;Group 2&quot;
Grid.Row=&quot;1&quot;
Grid.Column=&quot;0&quot;&gt;&lt;/Label&gt;
&lt;HorizontalStackLayout
Grid.Row=&quot;1&quot;
Grid.Column=&quot;1&quot;
RadioButtonGroup.GroupName=&quot;Group2&quot;&gt;
&lt;RadioButton
x:Name=&quot;Group2RedRadioButton&quot;
Content=&quot;Red&quot;
IsChecked=&quot;{Binding Path=Group2RedChecked}&quot;
Value=&quot;Red&quot;&gt;
&lt;/RadioButton&gt;
&lt;RadioButton
x:Name=&quot;Group2GreenRadioButton&quot;
Content=&quot;Green&quot;
IsChecked=&quot;{Binding Path=Group2GreenChecked}&quot;
Value=&quot;Green&quot;&gt;
&lt;/RadioButton&gt;
&lt;RadioButton
x:Name=&quot;Group2BlueRadioButton&quot;
Content=&quot;Blue&quot;
IsChecked=&quot;{Binding Path=Group2BlueChecked}&quot;
Value=&quot;Bule&quot;&gt;
&lt;/RadioButton&gt;
&lt;/HorizontalStackLayout&gt;
&lt;/Grid&gt;
&lt;/VerticalStackLayout&gt;
&lt;/ScrollView&gt;
&lt;/ContentPage&gt;

The view model has 5 boolean values:

    public class ViewModel : INotifyPropertyChanged
{
public bool Group1YesChecked { get; set; }
public bool Group1NoChecked { get; set; }
public bool Group2RedChecked { get; set; }
public bool Group2GreenChecked { get; set; }
public bool Group2BlueChecked { get; set; }
public ViewModel() 
{ 
Group1YesChecked = true;
Group1NoChecked = false;
Group2RedChecked = false;
Group2GreenChecked = true;
Group2BlueChecked = false;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

The code behind for the page sets the view model:

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
ViewModel vm = new ViewModel();
this.BindingContext = vm;
}
}

main page display when application is run

What am I doing wrong?

I expected the Yes button to be selected.

答案1

得分: 0

我可以在我的项目中重现你的问题。而且这个问题只在 Windows 平台上出现。即使我直接将它的值设置为 true,它也不起作用。

&lt;RadioButton
   x:Name=&quot;Group1YesRadioButton&quot;
   Content=&quot;是&quot;
   IsChecked=&quot;True&quot;
   Value=&quot;是&quot;&gt;
&lt;/RadioButton&gt;

我发现这是在 GitHub 上的一个已知问题。你可以关注这个问题:RadioButton GroupName 和 IsChecked 属性在 Windows 上工作不一致

英文:

I can reproduce your problem in my project. And this problem only appeared on the windows platform. Even though I set its value as true directly, it will not work either.

&lt;RadioButton
x:Name=&quot;Group1YesRadioButton&quot;
Content=&quot;Yes&quot;
IsChecked=&quot;True&quot;
Value=&quot;Yes&quot;&gt;
&lt;/RadioButton&gt;

And I found it is a known issue on the github. You can follow up this issue about RadioButton GroupName and IsChecked properties work inconsistently on Windows.

huangapple
  • 本文由 发表于 2023年7月13日 11:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定