首页 > 解决方案 > Xamarin.Form CollectionView 正在随机检查 RadioButtons

问题描述

几天来我一直试图弄清楚这个问题,但没有任何进展。

在我的项目中,我正在生成调查问卷,这通过一个带有 RadioButtons 的页面来实现 - 组。这些组在 CollectionView 中重复。

尽管每个 RadioButton 组都有自己的 GroupName,但在单击单选按钮并向下滚动时,也会单击其他单选按钮。此外,如果我来回滚动,则会单击其他单选按钮。

这对我来说真的很奇怪。最初我有以下选择(左图),上下滚动后,它看起来像右图。

在此处输入图像描述 在此处输入图像描述

代码真的很简单。

XAML 页面:

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Mobile.ViewModels"
             xmlns:commonLogic="clr-namespace:Mobile.Models;assembly=Mobile"
             xmlns:buttons="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"
             xmlns:behaviorsPack="clr-namespace:Xamarin.Forms.BehaviorsPack;assembly=Xamarin.Forms.BehaviorsPack"
             xmlns:resources="clr-namespace:Mobile;assembly=Mobile"
             NavigationPage.HasBackButton="False"
             NavigationPage.HasNavigationBar="False"
             x:Class="Mobile.Views.QuestionarePage"
             x:DataType="local:QuestionnaireViewModel"
             Shell.NavBarIsVisible="True"
             Shell.FlyoutBehavior="Disabled"
             Title="{x:Static resources:AppResources.QuestionnaireTitle}">
    <ContentPage.BindingContext>
        <local:QuestionnaireViewModel/>
    </ContentPage.BindingContext>
    <CollectionView x:Name="listView"  ItemsSource="{Binding StringQuestions}" SelectionMode="None">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout  Padding="10,5,10,8" Spacing="2" x:DataType="commonLogic:TestClass">
                        <Label Text="{Binding GroupName}" LineBreakMode="WordWrap" FontAttributes="Bold" TextColor="{StaticResource Primary}" FontSize="18"/>
                        <RadioButton  GroupName="{Binding GroupName}" Content="{Binding GroupName}"></RadioButton>
                        <RadioButton  GroupName="{Binding GroupName}"  Content="{Binding GroupName}"></RadioButton>
                        </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>

视图模型类:

public class TestClass 
{
public string GroupName { get; set; }

public TestClass(string name)
{
    GroupName = name;
}
}

任何帮助表示赞赏。谢谢

更新

Even following code is causing me troubles
    <CollectionView x:Name="listView" ItemsSource="{Binding Questions}" SelectionMode="None">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout  Padding="10,5,10,8" Spacing="2" >
                    <Label Text="TEST" LineBreakMode="WordWrap" FontAttributes="Bold" FontSize="18"/>
                    <RadioButton/>
                    <RadioButton/>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

标签: xamarin.formscollectionview

解决方案


您可以尝试以下两种方法。

ListView用来代替CollectionView

<ListView x:Name="listView"  HasUnevenRows="True" ItemsSource="{Binding StringQuestions}" SelectionMode="None" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                <StackLayout  Padding="10,5,10,8" Spacing="2" RadioButtonGroup.GroupName="{Binding GroupName}">
                    <Label Text="{Binding GroupName}" LineBreakMode="WordWrap" FontAttributes="Bold" TextColor="AliceBlue" FontSize="18"/>
                    <RadioButton   Content="{Binding GroupName}"></RadioButton>
                    <RadioButton   Content="{Binding GroupName}"></RadioButton>
                </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

:在模型中定义两个属性。

public class TestClass
{
    public string GroupName { get; set; }
    public bool IsFirstCheck { get; set; }
    public bool IsSecondCheck { get; set; }
    public TestClass(string name)
    {
        GroupName = name;
    }
}

然后绑定到您的 RadioButtons:

<CollectionView x:Name="listView"  ItemsSource="{Binding StringQuestions}" SelectionMode="None" >
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout  Padding="10,5,10,8" Spacing="2" RadioButtonGroup.GroupName="{Binding GroupName}">
                    <Label Text="{Binding GroupName}" LineBreakMode="WordWrap" FontAttributes="Bold" TextColor="AliceBlue" FontSize="18"/>
                    <RadioButton   IsChecked="{Binding IsFirstCheck}" CheckedChanged="RadioButton_CheckedChanged" Content="{Binding GroupName}"></RadioButton>
                    <RadioButton   IsChecked="{Binding IsSecondCheck}" CheckedChanged="RadioButton_CheckedChanged_1" Content="{Binding GroupName}"></RadioButton>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
</CollectionView>

在您的代码后面,更改IsFirstCheckIsSecondCheck值。

private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
    {
    
        TestClass test = (TestClass)(sender as RadioButton).BindingContext;
        if (test !=null)
        {
            test.IsFirstCheck = e.Value;
        }  
    
    }

private void RadioButton_CheckedChanged_1(object sender, CheckedChangedEventArgs e)
    {
        TestClass test = (TestClass)(sender as RadioButton).BindingContext;
        if (test != null)
        {
            test.IsSecondCheck = e.Value;
        }
    }

推荐阅读