更新时间:2023-12-05 15:40:40
向viewmodel添加第二个属性:public CartData SelectedCartData
并在XAML中绑定listview的Selected属性,确保将Mode设置为TwoWay。这样,绑定就是从cartData集合中提取数据并将所选值推回到viewmodel中。一旦发生这种情况,您可以致电:cartData.Remove(SelectedCartData);
尝试
绑定到ListBox(或其他项控件)的选定项目 [ ^ ]
ListView MultiSelect,MVVM和RoutedCommands [ ^ ]
MVVM多选列表框 [ ^
hello guys i was able to add items using this method
public ObservableCollection<CartData> cartData
{
get
{
if (App.Current._cartData == null)
{
App.Current._cartData = new ObservableCollection<CartData>();
}
return App.Current._cartData;
}
set
{
SetProperty(ref App.Current._cartData, value);
}
}
public CartData(){
cartData.Add(new CartData { Cakename = this.CakeName, Cakeprice = App.Current.cakeprice,ImagePath=bitmapimage,Caketype=App.Current.caketype,Cakesize=App.Current.cakesize });
}
it is binded to the listview, now im trying to remove the item if certain item is selected and pressed a button. How do i know the Index of the selected item in the list?
Add a second property to the viewmodel:public CartData SelectedCartData
and bind the Selected property of the listview in XAML making sure to set the Mode to TwoWay. This way the binding is pulling data from the cartData collection and pushing the selected value back into the viewmodel. Once this happens, you can then call:cartData.Remove(SelectedCartData);
Try
Binding to the selected items of a ListBox (or another items controls)[^]
ListView MultiSelect, MVVM and RoutedCommands[^]
MVVM Multiselect Listbox[^]