xamarin 表单更新 CollectionView 中的项目 [英] xamarin forms Update Item in CollectionView

查看:25
本文介绍了xamarin 表单更新 CollectionView 中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用集合视图创建 xmarin 表单应用程序包含两个项目(标签 = 绑定名称和开关 = 绑定已切换)但我有一个开关问题,它不会在不滚动集合的情况下更新我使用 Model-View-ViewModel 绑定我的数据并进行操作那是我的 xaml :

iam trying to create xmarin forms application witch use a collection view that contain two item ( label = binding name & switch = binding switched ) but i have a problem with the switch its not update without Scrolling the collection i use Model-View-ViewModel to bind my data and do operation that`s my xaml :

<CollectionView x:Name="GroupsCV"  
                        ItemsSource="{Binding Groups}" 
                        SelectionMode="Multiple"
                        EmptyView="No Data"
                        SelectionChangedCommand="{Binding SelectedCommand}"
                        SelectionChangedCommandParameter="{Binding Source={x:Reference GroupsCV}}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid Padding="15">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="0.1*"/>
                        </Grid.ColumnDefinitions>
                        <Switch Style="{StaticResource SwitchStyle}" IsEnabled="False" IsToggled="{Binding Switched}" Grid.Column="0"/>
                        <Label Text="{Binding Name}" Grid.Column="1" Style="{StaticResource LabelStyle}"/>
                    </Grid>
                </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

这是我来自视图模型的代码:

and this my code from View Model:

public GroupsViewModel()
    {
        datarepo.conn.CreateTable<Group>();
        _groups = datarepo.conn.Table<Group>().ToList();
        //GroupsCount = Groups.Count();
    }
    private IEnumerable<Group> _groups;
    public IEnumerable<Group> Groups
    {
        get => _groups;
        set 
        {
            if (_groups != value)
            {
                _groups = value;
                OnPropertyChanged(nameof(Groups));
            }
        }
    }

public ICommand SelectedCommand
    {
        get
        {
            return new Command<CollectionView>((s) =>
            {
                var x = s.SelectedItems.Cast<Group>();
                foreach (var e in x)
                {
                    foreach (var z in Groups)
                    {
                        if (z.Link == x..Link)
                        {
                            if (z.Switched == false)
                            {
                                z.Switched = true;
                            }
                            else
                            {
                                z.Switched = false;
                            }
                            break;
                        }
                    }
                }
                OnPropertyChanged(nameof(Groups));
            });
        }
    }

一切正常,只是用户界面不更新!

All Thing Work fine only the ui doesn`t update !

使用组代码更新

[Table("Group")]
public class Group
{
    [PrimaryKey, AutoIncrement()]
    public int? Id { get; set; }
    [Unique]
    public string Link { get; set; }
    public string Name { get; set; }
    public string MemberCount { get; set; } = null;
    public bool Switched { get; set; }

}

推荐答案

你能像这样改变你的 Group 类吗?

Can you change your Group class like this?

[Table("Group")]
public class Group: INotifyPropertyChanged
{
 public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    [PrimaryKey, AutoIncrement()]
    public int? Id { get; set; }
    [Unique]
    public string Link { get; set; }
    public string Name { get; set; }
    public string MemberCount { get; set; } = null;
    public bool Switched { get; set; }

}

这篇关于xamarin 表单更新 CollectionView 中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆