【问题标题】:WindowsPhone: IsSelected property doesn't change in collectionWindowsPhone:IsSelected 属性在集合中没有改变
【发布时间】:2016-12-11 04:58:20
【问题描述】:

我为 WindowsPhone 8.1 开发 MVVM crud 应用

App 向 ListBox 添加一些数据。每个项目都有复选框。 如果您选中或取消选中复选框 - 事件处理程序工作,属性更改值。 但是,IsSelected 属性的值在存储的集合中不会改变。

View.xaml - 复选框代码

  <CheckBox  x:Name="checkbox" IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<CheckBox.DataContext>
<loal:DataStorageModel/>
 </CheckBox.DataContext>
  </CheckBox> 

DataStorageModel.cs

  public class DataStorageModel : INotifyPropertyChanged
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }
        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }
            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

GitHub 上的所有代码 - https://github.com/OlegZarevych/CRUD_WP81

【问题讨论】:

    标签: c# checkbox windows-phone-8 mvvm windows-phone-8.1


    【解决方案1】:

    尝试删除 UpdateSourceTrigger=PropertyChanged,这应该可以解决您的问题

    【讨论】:

    • 谢谢。但也应该删除DataContext
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多