0

我有一个列表视图,在列表视图中我有一个输入框。我想在输入框变得不集中之后运行一些代码,我已经使用 xct 事件来命令行为,但我希望命令从列表视图中接收条目所在的对象。

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

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

4

1 回答 1

0

根据您的代码,我创建了一个简单的演示,它可以正常工作。

您可以参考以下代码:

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

视图模型是:

 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);
    }
}
于 2021-12-21T06:53:40.663 回答