Xamarin: ListView组头与复选框

问题描述 投票:0回答:1

在应用程序中,我有一个ListView,绑定了一些数据,并对其进行了分组,我需要在分组头中添加一个复选框,一旦选择了分组头复选框,则必须选择分组下面的所有数据。我已经在分组头添加了一个复选框,如

<xFormsListView:SfListView x:Name="SpecialityDiseaseListView"
                           itemsSource="{Binding SuggestionDiseaseList}" 
                           ItemSize="40" SelectionBackgroundColor="Transparent">
<xFormsListView:SfListView.DataSource>
    <dataSource:DataSource>
        <dataSource:DataSource.GroupDescriptors>
            <dataSource:GroupDescriptor PropertyName="GroupHeading"/>
        </dataSource:DataSource.GroupDescriptors>
    </dataSource:DataSource>
</xFormsListView:SfListView.DataSource>
<xFormsListView:SfListView.GroupHeaderTemplate>
    <DataTemplate>
        <ViewCell>
            <ViewCell.View>
                <StackLayout Padding="10,0,15,0">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="70*"/>
                            <ColumnDefinition Width="20*"/>
                            <ColumnDefinition Width="10*"/>
                        </Grid.ColumnDefinitions>
                        <Label Text="{Binding Key}"  FontSize="18" 
                               VerticalOptions="Center" HorizontalOptions="Start" 
                               Style="{StaticResource LabelProfileStyle}"/>
                        <buttons:SfCheckBox HorizontalOptions="End" Grid.Column="1" 
                                            IsChecked="{Binding IsSelected}"/>
                    </Grid>
                    <BoxView  HeightRequest=".5" BackgroundColor="Black" 
                              HorizontalOptions="FillAndExpand" />                                                               

                </StackLayout>
            </ViewCell.View>
        </ViewCell>
    </DataTemplate>
</xFormsListView:SfListView.GroupHeaderTemplate>
<xFormsListView:SfListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <StackLayout Orientation="Horizontal" VerticalOptions="FillAndExpand"
                                                             Padding="5,0,0,0" BackgroundColor="Transparent">
                <buttons:SfCheckBox Text="{Binding DisplayName}" IsChecked="{Binding IsSelected, Mode=TwoWay}" Style="{StaticResource ProfileCheckBoxStyle}"></buttons:SfCheckBox>
            </StackLayout>
        </ViewCell>
    </DataTemplate>
</xFormsListView:SfListView.ItemTemplate>

我想做一个组头检查,我可以实现所需的能力。Can any please suggest a way how I could Implement this.Thanks in advance.

c# xamarin xamarin.android syncfusion
1个回答
1
投票

你可以参考下面的文档,在SfListView GroupHeader中使用CheckBox。SfListView GroupHeader中的复选框。


0
投票

我会用一个 CollectionViewSelectionModeMultiple.

<CollectionView SelectionMode="Multiple" SelectedItems="{Binding SelectedItems}">
    <xFormsListView:SfListView.GroupHeaderTemplate>
        <DataTemplate>
            <StackLayout Padding="10,0,15,0">
                <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="70*"/>
                    <ColumnDefinition Width="20*"/>
                    <ColumnDefinition Width="10*"/>
                </Grid.ColumnDefinitions>
                <Label Text="{Binding Key}" FontSize="18" VerticalOptions="Center"
                       HorizontalOptions="Start" Style="{StaticResource LabelProfileStyle}"/>
                <CheckBox Grid.Column="2" IsChecked="{Binding IsChecked}"/>
                </Grid>
                <BoxView HeightRequest=".5" BackgroundColor="Black" 
                                 HorizontalOptions="FillAndExpand" />
            </StackLayout>
        </DataTemplate>
    </xFormsListView:SfListView.GroupHeaderTemplate>
</CollectionView>

ViewModel。

public ObservableCollection<object> SelectedItems { get; set; } = new ObservableCollection<object>();

视图模型 SelectedItems 属性的类型必须是 ObservableCollection<object> 或者它不会工作。Xamarin.Forms 4.6中会支持自定义对象,但目前还不存在。然后,用户可以选择他们想要的项目,或者你可以使用一个复选框以编程方式将项目放到 SelectedItems.

延长 CheckBox 接受一个字符串,这样你就可以通过组名,知道哪些项目要放在 SelectedItems.

© www.soinside.com 2019 - 2024. All rights reserved.