Xamarin窗体:如何打开Listview中的所有开关?

Xamarin窗体:如何打开Listview中的所有开关?,listview,xamarin.forms,Listview,Xamarin.forms,我在列表视图中有一个开关。从DisplayActionSheet中选择选项时,我需要打开所有开关。我怎样做这个功能 Xaml代码: <ListView x:Name="UserList"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View>

我在列表视图中有一个开关。从
DisplayActionSheet
中选择选项时,我需要打开所有开关。我怎样做这个功能

Xaml代码:

<ListView 
    x:Name="UserList">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout
                        HorizontalOptions="FillAndExpand"
                        Orientation="Horizontal">
                        <Label 
                            Text="{Binding fullname}"
                            Font="11" 
                            TextColor="Black"
                            HorizontalOptions="Start" 
                            VerticalOptions="Center"/> 

                        <Switch 
                            HorizontalOptions="EndAndExpand"
                            VerticalOptions="CenterAndExpand"/>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

更新至Nikil

添加了切换的
IsToggled=“{Binding IsSelected}”
,并在模型中添加了
public bool IsSelected{get;set;}
。但是在
DisplayActionSheet
中添加的代码出现错误。添加以下内容的屏幕截图:


在绑定ListView的模型中,可以有一个名为IsSelected的bool属性。 该模型应实现INotifyPropertyChanged。 然后可以绑定IsToggled=“{Binding IsSelected}”

在上,从DisplayActionSheet中选择该选项

List ItemSource = ItemSource(x => {x. IsSelected = true; return x;}).ToList()

在绑定ListView的模型中,可以有一个名为IsSelected的bool属性。 该模型应实现INotifyPropertyChanged。 然后可以绑定IsToggled=“{Binding IsSelected}”

在上,从DisplayActionSheet中选择该选项

List ItemSource = ItemSource(x => {x. IsSelected = true; return x;}).ToList()

从讨论的共享示例项目中,绑定模型是web API中的webContentList。如果想要选定的方法工作,首先需要在其中添加属性(
IsSelected
),并添加INotifyPropertyChanged

public class webContentList : INotifyPropertyChanged
{
    public string swcmMessage { get; set; }
    public string pageTitle { get; set; }
    public Modifier modifier { get; set; }
    public bool deletable { get; set; }
    public string pageContentType { get; set; }
    public string previewUUID { get; set; }
    public int webContentDefinitionId { get; set; }
    public long pageCreatedTime { get; set; }
    public int userCreated { get; set; }
    public string pageStatus { get; set; }
    public string thumbnailImageUrl { get; set; }
    public string videoUrl { get; set; }
    public string imageUrl { get; set; }
    public int webContentId { get; set; }
    public Creator creator { get; set; }
    public string contentTemplateId { get; set; }
    public string appName { get; set; }
    public string customHTML { get; set; }
    public string processedTime { get; set; }
    public string pageDesc { get; set; }
    public string editUrl { get; set; }
    public string pageKwd { get; set; }
    public bool staticContent { get; set; }
    public int siteId { get; set; }
    public string pageUrl { get; set; }
    public string linkType { get; set; }
    public long pageUpdatedDate { get; set; }
    public string swcmStatus { get; set; }
    public int userModified { get; set; }
    public string value { get; set; }
    public bool isProfileImageNull { get { return string.IsNullOrEmpty(thumbnailImageUrl); } }

    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;
            OnPropertyChanged(nameof(IsSelected));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
然后在菜单选项方法中,可以修改如下:

public async void MenuOptions(object sender, EventArgs e)
{
    string action = await DisplayActionSheet(null, "Cancel", null, "Select All");
    if (action == "Select All")
    {
        //List<TweetTopicsResponse> ItemSource = ItemSource(x => { x.IsSelected = true; return x; }).ToList();
        List<webContentList> itemSource = AdminTopicList.ItemsSource as List<webContentList>;
        foreach(webContentList item in itemSource)
        {
            item.IsSelected = true;
        }
    }
}
public async void菜单选项(对象发送方、事件参数)
{
字符串操作=等待显示操作表(null,“取消”,null,“全选”);
如果(操作==“全选”)
{
//List ItemSource=ItemSource(x=>{x.IsSelected=true;返回x;});
List itemSource=AdminTopicList.ItemsSource作为列表;
foreach(itemSource中的webContentList项)
{
item.IsSelected=true;
}
}
}


这里是供参考的

从讨论的共享示例项目中,绑定模型是web API中的webContentList。如果想要选定的方法工作,首先需要在其中添加属性(
IsSelected
),并添加INotifyPropertyChanged

public class webContentList : INotifyPropertyChanged
{
    public string swcmMessage { get; set; }
    public string pageTitle { get; set; }
    public Modifier modifier { get; set; }
    public bool deletable { get; set; }
    public string pageContentType { get; set; }
    public string previewUUID { get; set; }
    public int webContentDefinitionId { get; set; }
    public long pageCreatedTime { get; set; }
    public int userCreated { get; set; }
    public string pageStatus { get; set; }
    public string thumbnailImageUrl { get; set; }
    public string videoUrl { get; set; }
    public string imageUrl { get; set; }
    public int webContentId { get; set; }
    public Creator creator { get; set; }
    public string contentTemplateId { get; set; }
    public string appName { get; set; }
    public string customHTML { get; set; }
    public string processedTime { get; set; }
    public string pageDesc { get; set; }
    public string editUrl { get; set; }
    public string pageKwd { get; set; }
    public bool staticContent { get; set; }
    public int siteId { get; set; }
    public string pageUrl { get; set; }
    public string linkType { get; set; }
    public long pageUpdatedDate { get; set; }
    public string swcmStatus { get; set; }
    public int userModified { get; set; }
    public string value { get; set; }
    public bool isProfileImageNull { get { return string.IsNullOrEmpty(thumbnailImageUrl); } }

    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;
            OnPropertyChanged(nameof(IsSelected));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
然后在菜单选项方法中,可以修改如下:

public async void MenuOptions(object sender, EventArgs e)
{
    string action = await DisplayActionSheet(null, "Cancel", null, "Select All");
    if (action == "Select All")
    {
        //List<TweetTopicsResponse> ItemSource = ItemSource(x => { x.IsSelected = true; return x; }).ToList();
        List<webContentList> itemSource = AdminTopicList.ItemsSource as List<webContentList>;
        foreach(webContentList item in itemSource)
        {
            item.IsSelected = true;
        }
    }
}
public async void菜单选项(对象发送方、事件参数)
{
字符串操作=等待显示操作表(null,“取消”,null,“全选”);
如果(操作==“全选”)
{
//List ItemSource=ItemSource(x=>{x.IsSelected=true;返回x;});
List itemSource=AdminTopicList.ItemsSource作为列表;
foreach(itemSource中的webContentList项)
{
item.IsSelected=true;
}
}
}


这里是供参考的

你能再解释一下吗,我没有得到第一部分。你需要有一个类,它的属性字符串为fullName,bool为selected。该类需要实现BaseViewModel或ObservableObject,后者反过来实现INotifyPropertyChanged。使用屏幕截图更新了问题,请看一看。您使用的模型类是什么?我在尖括号中添加了模型类名,但是使用未赋值的局部变量'ItemSource'error你能解释一下吗,我没有得到第一部分。你需要一个类,它的属性字符串为fullName,bool为selected。该类需要实现BaseViewModel或ObservableObject,后者反过来实现INotifyPropertyChanged。使用屏幕截图更新了问题,请看一看。您使用的模型类是什么?我在尖括号中添加了模型类名,但使用未分配的局部变量“ItemSource”时出错