WIll Cruz
WIll Cruz

Reputation: 73

Sending selected object from listview on entry unfocused through event to command behavior

I have a listview, within the list view I have an entry box. I want to run some code after the entry box becomes unfocused which I have got working with the xct event to command behavior, but I would like the command to receive the object from the listview that the entry is inside.

(sudo xaml)

<ListView Name = ReportGrid>
    <DataTemplate>
        <ViewCell>
            <StackLayout>
                <Entry Text="0.00" VerticalOptions="Center" FontSize="16" BackgroundColor="White" Margin="10, 0, 0, 0">
                    <Entry.Behaviors
                        <behaviors:EventToCommandBehavior EventName="Unfocused" Command="{Binding Source={Reference CountLocationTabView}, Path=BindingContext.QtyEntryUnfocused}" 
                                                          CommandParameter="{Binding Source={Reference ReportGrid}, Path=SelectedItem}" />
                    </Entry.Behaviors>
                </Entry>
            <StackLayout/>
        <ViewCell/>
    <DataTemplate/>
<ListView/>

I have seen examples of this where events from the listview are turned into commands but what about the elements within? thanks in advance.

Upvotes: 0

Views: 331

Answers (1)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13853

Based on your code,I created a simple demo, and it works properly.

You can refer to the following code:

        <ListView ItemsSource="{Binding items}" HasUnevenRows="True" HeightRequest="240" x:Name="Listview">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Frame Padding="0" >
                            <Grid Padding="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="0.5*"></ColumnDefinition>
                                    <ColumnDefinition Width="3*"></ColumnDefinition>
                                    <ColumnDefinition Width="3*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Label x:Name="DistNumber" Text="{Binding DistNumber}" FontSize="18"   Grid.Column="1" HorizontalTextAlignment="Start" VerticalOptions="Center"/>
                                <Entry  x:Name="CountEntry" Placeholder="Choose Quantity" 
                                        Text="{Binding Count}" 
                                        
                                        Grid.Column="2" HorizontalTextAlignment="Center" >
                                    <Entry.Behaviors>
                                        <behaviors:EventToCommandBehavior 
                                            EventName="Unfocused" 
                                            Command="{Binding Path=BindingContext.EntryUnfocused, Source={x:Reference Listview}}" CommandParameter="{Binding .}" />

                                    </Entry.Behaviors>
                                </Entry>
                               
                            </Grid>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

The ViewModel is:

 public class MyViewModel
{
    public ObservableCollection<SomeItem> items { get; set; }

    public ICommand EntryUnfocused { get; protected set; }


    public MyViewModel() {

        items = new ObservableCollection<SomeItem>();

        items.Add(new SomeItem { Name = "item1", DistNumber = "1", Count = 1,Quantity = 1 });
        items.Add(new SomeItem { Name = "item2", DistNumber = "2", Count = 2, Quantity = 2 });
        items.Add(new SomeItem { Name = "item3", DistNumber = "3", Count = 3, Quantity = 3 });
        items.Add(new SomeItem { Name = "item4", DistNumber = "4", Count = 4, Quantity = 4 });
        items.Add(new SomeItem { Name = "item5", DistNumber = "5", Count = 5, Quantity = 5 });
        items.Add(new SomeItem { Name = "item6", DistNumber = "6", Count = 6, Quantity = 6 });


        EntryUnfocused = new Command(CompletedCommandExecutedAsync);
    }

    private void CompletedCommandExecutedAsync(Object obj)
    {
        SomeItem item = (SomeItem)obj;

        System.Diagnostics.Debug.WriteLine("<----- item Name = " + item.Name + "<-----> item Count = " + item.Count);
    }
}

Upvotes: 0

Related Questions