[英]Using WPF/MVVM, how do I pass the SelectedIndex value of a Listbox to my ViewModel?
[英]How do I pass the parameter of the Context Menu ListView to my ViewModel?
我必須創建帶有刪除選項的菜單。
我在Visual Studio Update 3中使用Xamarin。
我的XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Refeicao.View.ListaRefeicaoView"
Title="Lista de Refeições">
<ContentPage.Content>
<ListView ItemsSource="{Binding Refeicoes}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Apagar" Command="{Binding RemoveRefeicao}" CommandParameter="{Binding .}"/>
</ViewCell.ContextActions>
<StackLayout>
<Label Text="{Binding Descricao, Mode=TwoWay}"/>
<StackLayout>
<Label Text="Calorias: " />
<Label Text="{Binding Calorias}" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
我的XAML.cs
namespace Refeicao.View
{
public partial class ListaRefeicaoView : ContentPage
{
public ListaRefeicaoView(RefeicaoDAO refeicaoDao)
{
CadastroRefeicaoViewModel cadastroRefeicaoViewModel = new CadastroRefeicaoViewModel(refeicaoDao, this);
BindingContext = cadastroRefeicaoViewModel;
InitializeComponent();
}
}
}
還有我的ViewModel類
namespace Refeicao.ViewModel
{
public class CadastroRefeicaoViewModel : INotifyPropertyChanged
{
private RefeicaoDAO _refeicaoDao;
private ContentPage _contentPage;
public event PropertyChangedEventHandler PropertyChanged;
private string _descricao;
private double _calorias;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Descricao
{
get { return _descricao; }
set
{
if (Equals(_descricao, value)) return;
_descricao = value;
OnPropertyChanged(nameof(Descricao));
}
}
public double Calorias
{
get { return _calorias; }
set
{
if(Equals(_calorias, value)) return;
_calorias = value;
OnPropertyChanged(nameof(Calorias));
}
}
public ObservableCollection<RefeicaoModel> Refeicoes { get; set; }
public Command SalvaRefeicao { get; private set; }
public Command RemoveRefeicao { get; private set; }
public CadastroRefeicaoViewModel(RefeicaoDAO refeicaoDao, ContentPage contentPage)
{
_refeicaoDao = refeicaoDao;
_contentPage = contentPage;
Refeicoes = _refeicaoDao.Refeicoes;
SalvaRefeicao = new Command(() =>
{
RefeicaoModel refeicao = new RefeicaoModel() { Descricao = _descricao, Caloria = _calorias };
_refeicaoDao.Salvar(refeicao);
string msg = $"A refeição {_descricao} de {_calorias} foi salva.";
_contentPage.DisplayAlert("Refeição Salva", msg, "OK");
});
RemoveRefeicao = new Command(() =>
{
RefeicaoModel refeicao = new RefeicaoModel() { Descricao = _descricao, Caloria = _calorias };
_refeicaoDao.Remover(refeicao);
string msg = $"A refeição {_descricao} foi removida.";
_contentPage.DisplayAlert("Refeição Removida", msg, "OK");
});
}
}
}
還有沒有后面的代碼怎么辦呢? 您是否只想使用Tapped
運行此命令?
您只需要為Command
使用正確的構造函數即可 。
代替:
RemoveRefeicao = new Command(() =>
{
}
這將是:
RemoveRefeicao = new Command(p =>
{
var yourData = (RefeicaoModel)p;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.