Remove ListBoxItem from ViewModel

时间:2016-07-11 22:31:04

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

I develop CRUD app for WindowsPhone 8.1. I can add data to ObservableCollection collection and this data is displayed on ListBox. I use MVVM pattern.

Full repository https://github.com/OlegZarevych/CRUD_WP81

View :

<ListBox x:Name="Storage" ItemsSource="{Binding Path=Models, Mode=TwoWay}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid  Height="30" Width="450">
                            <TextBlock x:Name="nameblock" Text="{Binding Name}" />
                        </Grid>
                        </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

And ViewModel class

 public class ViewModel
    {
        public string NewName { get; set; }
        public string NewSurname { get; set; }
        public int NewAge { get; set; }
        public int i=0 ;
        public ObservableCollection<DataStorageModel> Models { get; set; }
        //Event Handlers
        public ICommand CreateClickCommand { get; set; }
        public ICommand UpdateClickCommand { get; set; }
        public ICommand DeleteClickCommand { get; set; }
        public ViewModel()
        {
            CreateClickCommand = new RelayCommand(arg => CreateClickMethod());
            UpdateClickCommand = new RelayCommand(arg => UpdateClickMethod());
            DeleteClickCommand = new RelayCommand(arg => DeleteClickMethod());

            Models = new ObservableCollection<DataStorageModel>()         {};
        }
        private void CreateClickMethod()
        {
            Models.Add(new DataStorageModel() { Name = NewName, Surname = NewSurname, Age = NewAge, Count=i++ });

        }
        private void UpdateClickMethod()
        {}
        private void DeleteClickMethod()
       {}

}

I want to change data and delete it. As i good understand, I need select count from ListBoxItems and delete(update) this count in ObservableCollection. How can I work with XAML code from ViewModel class ? How can I initiliaze Storage in ViewModel ?

Or in MVVM is the better way to resolve this problem ?

2 个答案:

答案 0 :(得分:0)

如果要从ListBox删除模型,通常需要某种方法来识别要删除的所选ListBoxItems(或模型);为此,请考虑在模型上使用IsSelected属性并将其绑定到CheckBox数据模板中的ListBoxItem

现在,当您单击“删除”时,删除命令可以轻松查看Models列表,并查看选择要删除的项目。删除项目后,它可以枚举集合并重新计算剩余项目的计数值,并更新视图模型中的字段。

因此,您无需访问XAML即可更新模型的数量。如果您使count属性可变,那么从列表中删除项目后就不必重新初始化存储。

答案 1 :(得分:0)

我在模型

中添加了代码
create

还添加了bindin to View的复选框。

    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));
        }
    }

但是当我勾选项目中的复选框时,IsSelected var不会改变 为什么?