xamarin - 如何将 Xamarin Forms Checkbox isChecked 绑定(bind)到动态 bool 变量?

标签 xamarin xamarin.forms

我是 xamarin 表单的新手。我有一个包含复选框的 ListView 。我将复选框“isChecked”绑定(bind)到 ListView 的 itemsource bool 属性之一。问题是,每次我更改绑定(bind)复选框的 bool 值时,复选框的外观都没有改变。我怎样才能实现这种方法? enter image description here

[1]: /image/4KcT2.png

最佳答案

enter image description here嗨@Weggie Villarante。请试试这个。它对我有用

     <ViewCell>
                            <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Orientation="Horizontal">
                                <Label Text="{Binding Title}" HorizontalOptions="StartAndExpand"></Label>
                                <CheckBox IsChecked="{Binding IsCheck}" HorizontalOptions="End" HeightRequest="50"></CheckBox>
                            </StackLayout>
                        </ViewCell>
NotificationModel.cs


    public class NotificationModel : INotifyPropertyChanged
        {
            public string Message { get; set; }
            public string  Title { get; set; }
            public bool _IsCheck = false;

            public bool IsCheck
            {
                get
                {
                    return _IsCheck;
                }
                set
                {
                    _IsCheck = value;
                    this.OnPropertyChanged("IsCheck");
                }
            }


            public event PropertyChangedEventHandler PropertyChanged;
            void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

NotificationViewModel.cs

 class NotificationViewModel : INotifyPropertyChanged
    {
        ObservableCollection<NotificationModel> _Items;
        public ObservableCollection<NotificationModel> Items
        {
            get
            {
                return _Items;
            }
            set
            {
                _Items = value;
                OnPropertyChanged();
            }
        }

        public NotificationViewModel()
        {
            Items = new ObservableCollection<NotificationModel>();
            AddItems();
        }
        void AddItems()
        {
            _Items.Add(new NotificationModel { Title = "Info", Message = "This is only information message please ignor this one." ,IsCheck = false});
            _Items.Add(new NotificationModel { Title = "Alert", Message = "This is only Alert message please ignor this one." , IsCheck = false });
            _Items.Add(new NotificationModel { Title = "Suggesstion", Message = "This is only Suggesstion message please ignor this one." , IsCheck = false});
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

关于xamarin - 如何将 Xamarin Forms Checkbox isChecked 绑定(bind)到动态 bool 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59297526/

相关文章:

xamarin.forms - "Control"自定义渲染器和效果中的属性对于布局为空

c# - 从ViewModel调用方法背后的代码

android - 使用 Google Material Design 的全宽 ActionBar

android - MVVMCross System.AggregateException 6.4 Xamarin Android

c# - 在 Xamarin 中获取位置 GPS 数据时无法在线程内创建处理程序

android - Xamarin Forms 将 WebView 内容获取为字符串

c# - 如何编写一个在 C# 中打开图像的方法,该方法将使用移动设备上的默认图像查看器...?

ios - xamarin 构建在模拟器上通过但在真实设备上失败

c# - Visual Studio 2017无法连接macOS虚拟机安装visual studio for mac和xcode

android - Xamarin.Forms(以 PCL 代码从平台项目中读取文件)