通过事件未聚焦的条目从列表视图发送选定对象到命令行为

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

我有一个列表视图,在列表视图中我有一个输入框。我想 运行 在输入框失去焦点后编写一些代码,我已经使用 xct 事件来命令行为,但我希望该命令从列表视图中接收该条目所在的对象。

(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/>

我见过这样的示例,其中列表视图中的事件被转换为命令,但是其中的元素呢? 提前致谢。

根据你的代码,我制作了一个简单的demo,运行正常。

您可以参考以下代码:

        <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>

ViewModel 是:

 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);
    }
}