如何在 Xamarin Forms 中的 ListView 中添加复选框

4

我希望在XAML中使用Listview创建复选框,选择多个复选框后,单击按钮以获取所有已选择的值。我的代码如下:

<ContentPage.Content>
    <ListView  x:Name="ProductsListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout Spacing="0">
                            <Label Text="{Binding retail_modified_item_id}" 
                               TextColor="Blue" FontSize="13" />
                            <Label Text="{Binding name, StringFormat='Name : 
                               {0:N}'}" TextColor="black" FontSize="10"  />
                            <Label Text="{Binding retail_price, 
                               StringFormat='Price : {0:N}'}" 
                               TextColor="black" FontSize="10" />
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>                            
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

我的输出是; 没有复选框的输出

我尝试了一些示例代码,来自https://www.ignatiuz.com/blog/xamarin/button-check-box-with-list-item-in-xamarin-forms-listview/

但我得到了未找到input:checkbox

我是Xamarin的新手,请提供一些示例代码或链接以完成此操作

4个回答

2
你可以从Nuget使用Plugin.InputKit

用法

<?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:xxx"
             xmlns:input="clr-namespace:Plugin.InputKit.Shared.Controls;assembly=Plugin.InputKit"
             x:Class="xxx.MainPage">


<ContentPage.Content>
 <ListView  x:Name="ProductsListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Spacing="0">
                        <Label Text="{Binding retail_modified_item_id}" 
                           TextColor="Blue" FontSize="13" />
                        <Label Text="{Binding name, StringFormat='Name : 
                           {0:N}'}" TextColor="black" FontSize="10"  />
                        <Label Text="{Binding retail_price, 
                           StringFormat='Price : {0:N}'}" 
                           TextColor="black" FontSize="10" />
                        <input:CheckBox Text="xxx" Type="Check"/>  
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>                            
        </DataTemplate>
    </ListView.ItemTemplate>
 </ListView>
</ContentPage.Content>        

</ContentPage>

@AnuPriya,你能提供样例吗? - Lucas Zhang

1

1

使用OnCheckChanged方法向本地集合添加/删除值。我使用的方式如下:

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal">
                    <Label Text="Some item" />
                    <CheckBox 
                        IsChecked="true" 
                        CheckedChanged="OnCheckedChanged" 
                        HorizontalOptions="EndAndExpand"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我不是 nuGet 的忠实粉丝,除非没有其他选择。我知道这是有争议的,也许我在这方面是错误的,但无论如何,谢谢你的回答。 - Daniel Dolz

0
如果您需要在列表视图中使用复选框,可以使用Xamarin.Forms.InputKit(https://github.com/enisn/Xamarin.Forms.InputKit)。
然后,我会在DataTemplate的ViewCell中这样定义它:
 <input:CheckBox Text="Hello World I'm Option 2" Type="Check"/>

此外还有可绑定属性和事件,您可以根据不同场景使用:

CheckChanged: (事件) 在选中状态改变时调用。

CheckChangedCommand: (命令) 可绑定的命令,在选中状态改变时执行。

Key: (int) 可设置的键,用于定义复选框的ID。

Text: (string) 显示描述文本。

IsChecked: (bool) 复选框的选中情况。默认双向绑定。

Color: (Color) 选中复选框的颜色。

TextColor: (Color) 描述文本的颜色。

Type: (CheckType) 复选框选中样式的类型。(Check,Cross,Star,Box 等)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接