You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI桌面应用CollectionView嵌套mct:Expander异常问题求助

问题分析与解决方案

你遇到的异常是CollectionView的控件复用机制引发的,再加上模板内控件命名冲突、全局RadioButton组、未绑定Expander状态这几个问题叠加,才会出现点击底部展开顶部、内容空白的混乱现象。以下是针对性的解决步骤:

1. 删除DataTemplate内所有控件的x:Name

CollectionView会复用ItemTemplate中的控件,同一个模板里的x:Name会在多个Item实例中重复,导致MAUI无法正确跟踪每个控件的状态。直接删掉questionz、conforme、nonConforme、nonFait这些命名即可。

2. 给Question模型添加IsExpanded属性并绑定

每个Expander的展开状态需要和对应的数据项绑定,避免复用串位:

// 在你的Question模型类中添加(需实现INotifyPropertyChanged)
private bool _isExpanded;
public bool IsExpanded
{
    get => _isExpanded;
    set => SetProperty(ref _isExpanded, value);
}

修改XAML中的Expander:

<mct:Expander IsExpanded="{Binding IsExpanded}">

3. 让RadioButton组每个Item独立

原来的GroupName="questionAnswer"是全局的,所有Item的RadioButton都在同一个组,选一个其他Item的选项会被取消,且复用会导致状态混乱。改成绑定每个Question的唯一标识(比如Id):

<HorizontalStackLayout RadioButtonGroup.GroupName="{Binding Id}">
    <RadioButton GroupName="{Binding Id}" Content="Conforme" Value="1" />
    <RadioButton GroupName="{Binding Id}" Content="Non conforme" Value="0" />
    <RadioButton GroupName="{Binding Id}" Content="Non Fait" Value="" />
</HorizontalStackLayout>

4. 替换事件绑定为MVVM方式(推荐)

CheckedChanged事件在控件复用的时候容易出问题,建议直接绑定到Question模型的属性:

// 在Question模型中添加
private string _selectedAnswer;
public string SelectedAnswer
{
    get => _selectedAnswer;
    set => SetProperty(ref _selectedAnswer, value);
}

修改RadioButton的绑定:

<HorizontalStackLayout
    RadioButtonGroup.GroupName="{Binding Id}"
    RadioButtonGroup.SelectedValue="{Binding SelectedAnswer}">
    <RadioButton Content="Conforme" Value="1" />
    <RadioButton Content="Non conforme" Value="0" />
    <RadioButton Content="Non Fait" Value="" />
</HorizontalStackLayout>

这样就不需要后台的RadioButton_CheckedChanged事件,状态直接同步到ViewModel。

修改后的完整XAML示例

<CollectionView x:Name="question" ItemsSource="{Binding Audit.Questions}" IsVisible="false" WidthRequest="800">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="model:Question">
            <VerticalStackLayout>
                <mct:Expander IsExpanded="{Binding IsExpanded}">
                    <mct:Expander.Header>
                        <Grid BackgroundColor="LightGray" Padding="10" HeightRequest="100" MinimumHeightRequest="100">
                            <Label Text="{Binding Intitule}" FontSize="16" Grid.Column="0"></Label>
                            <Image Source="exclamation_mark.png"
                                   HeightRequest="20" 
                                   TranslationX="380"
                                   ToolTipProperties.Text="{Binding RappelJuridique}"/>
                        </Grid>
                    </mct:Expander.Header>
                    <mct:Expander.Content>
                        <StackLayout MinimumHeightRequest="150">
                            <HorizontalStackLayout
                                RadioButtonGroup.GroupName="{Binding Id}"
                                RadioButtonGroup.SelectedValue="{Binding SelectedAnswer}">
                                <RadioButton Content="Conforme" Value="1" />
                                <RadioButton Content="Non conforme" Value="0" />
                                <RadioButton Content="Non Fait" Value="" />
                            </HorizontalStackLayout>
                            <Entry Placeholder="Commentaire" Text="{Binding Commentaire}" HeightRequest="100" />
                        </StackLayout>
                    </mct:Expander.Content>
                </mct:Expander>
            </VerticalStackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

内容的提问来源于stack exchange,提问作者BarryF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:09:59