Как запустить команду при изменении выбранного элемента выбора - PullRequest
1 голос
/ 15 января 2020

Я пытаюсь запустить Command в моем ViewModel при изменении выбранного элемента, передавая весь объект в качестве параметра. SelectedCondicaoPagamento является значением по умолчанию. Как я могу это сделать?

Model

public class CaptacaoPedidoItem : Notifier
{
    //displays all 'CondicaoPagamento'
    public List<CondicaoPagamento> CondicoesPagamento { get; set; }

    private CondicaoPagamento _selectedCondicaoPagamento;

    public CondicaoPagamento SelectedCondicaoPagamento
    {
        get
        {
            return _selectedCondicaoPagamento;
        }
        set
        {
            _selectedCondicaoPagamento= value;
            NotifyPropertyChanged();
        }
    }
}

ViewModel

 public class CaptacaoViewModel : Notifier
 {
    public Command PlanoPagamentoAlteradoCommand => new Command(PlanoPagamentoAlterado);
    public List<CaptacaoPedidoItem> ItensPedido
    {
        get
        {
            return _itensPedido;
        }
        set
        {
            _itensPedido = value;
            NotifyPropertyChanged();
        }
    }
    //it's not called
    public void PlanoPagamentoAlterado()
    {

    }
 }

View

<ListView  Grid.Row="3"  x:Name="listasugestao" ItemsSource="{Binding ItensPedido}" RowHeight="80">
   {...}
   <Picker   
    ItemsSource="{Binding CondicoesPagamento}"
    SelectedItem="{Binding SelectedCondicaoPagamento}"
    ItemDisplayBinding="{Binding NomePlanoPagamento}"
    Grid.Column="6" VerticalOptions="Center" HorizontalOptions="FillAndExpand">

        <Picker.Behaviors>
            <behaviors:EventHandlerBehavior EventName="SelectedIndexChanged">
                <behaviors:InvokeCommandAction Command="{Binding PlanoPagamentoAlteradoCommand}" />
            </behaviors:EventHandlerBehavior>
        </Picker.Behaviors>
    </Picker>

</ListView>

1 Ответ

1 голос
/ 15 января 2020

В этом решении используется повторно используемое EventToCommandBehavior , опубликованное Microsoft. Я взял на себя смелость также посылать viewmodel, связанный с соответствующим cellview, в качестве параметра команды. Таким образом, легко узнать, в какой ячейке вашего списка индекс фактически изменился.

Просмотр

<Picker   
    ItemsSource="{Binding CondicoesPagamento}"
    SelectedItem="{Binding SelectedCondicaoPagamento}"
    ItemDisplayBinding="{Binding NomePlanoPagamento}"
    Grid.Column="6" VerticalOptions="Center" HorizontalOptions="FillAndExpand">

    <Picker.Behaviors>
        <behaviors:EventToCommandBehavior 
                EventName="SelectedIndexChanged" 
                Command="{Binding Path=BindingContext.PlanoPagamentoAlteradoCommand, Source={x:Reference Name=listasugestao}}"
                CommandParameter="{Binding}"/>
    </Picker.Behaviors>

</Picker>

ViewModel

public class CaptacaoViewModel : Notifier
{
    public CaptacaoViewModel()
    {
        PlanoPagamentoAlteradoCommand = new Command<CaptacaoPedidoItem>(WhenSelectedIndexChanged);
    }

    public Command PlanoPagamentoAlteradoCommand {get;}

    public List<CaptacaoPedidoItem> ItensPedido
    {
        get
        {
            return _itensPedido;
        }
        set
        {
            _itensPedido = value;
            NotifyPropertyChanged();
        }
    }

    private void WhenSelectedIndexChanged(CaptacaoPedidoItem item)
    {
        // do something
    }
 }
...