[英]Remove ListBoxItem from ViewModel
I develop CRUD app for WindowsPhone 8.1. 我为WindowsPhone 8.1开发CRUD应用程序。 I can add data to ObservableCollection collection and this data is displayed on ListBox.
我可以将数据添加到ObservableCollection集合,并且此数据显示在ListBox上。 I use MVVM pattern.
我使用MVVM模式。
Full repository https://github.com/OlegZarevych/CRUD_WP81 完整的存储库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 和ViewModel类
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.
据我所了解,我需要从ListBoxItems中选择计数并在ObservableCollection中删除(更新)此计数。 How can I work with XAML code from ViewModel class ?
如何使用ViewModel类中的XAML代码? How can I initiliaze Storage in ViewModel ?
如何初始化ViewModel中的存储?
Or in MVVM is the better way to resolve this problem ? 还是在MVVM中解决此问题的更好方法?
When you want to delete a model from the ListBox
you typically need some way to identify the selected ListBoxItems
(or models) that you want to delete; 当您想从
ListBox
删除模型时,通常需要一些方法来标识要删除的选定ListBoxItems
(或模型)。 for that, consider having an IsSelected
property on your models and bind it to a CheckBox
inside the ListBoxItem
data template. 为此,考虑接受
IsSelected
您的模型属性,并将其绑定到CheckBox
内部ListBoxItem
数据模板。
Now, when you click on delete, the delete command can then easily look into the Models
list and see which items are selected for deletion. 现在,当您单击删除时,delete命令可以轻松地查看“
Models
列表,并查看选择要删除的项目。 After it deletes the items, it can then enumerate over the collection and recalculate the count value for the remaining items and update the field in the view model. 删除项目后,它可以对集合进行枚举并重新计算剩余项目的计数值,并更新视图模型中的字段。
So, you don't have to access the XAML to update the count of the models. 因此,您不必访问XAML即可更新模型的数量。 If you make the count property mutable then you wouldn't have to reinitialize the storage after you delete items from the list.
如果您使count属性可变,那么从列表中删除项目后,您不必重新初始化存储。
I added code t the Model 我在模型中添加了代码
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));
}
}
Also added checkbox with bindin to View. 还向View添加了带有bindin的复选框。
<ListBox x:Name="Storage" Background="Gray" FontSize="14" ItemsSource="{Binding Path=Models, Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="60" Width="400" >
<CheckBox x:Name="checkbox" IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock x:Name="nameblock" Text="{Binding Name}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But IsSelected var doesn't change when I check checkbox in item Why ? 但是,当我选中“为什么”项中的复选框时,IsSelected var不会改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.