Привязка команд (с RelativeSourceExtension) в StackLayout с использованием BindableLayout.ItemsSource - PullRequest
0 голосов
/ 12 марта 2020

Я пытаюсь привязать команду (с RelativeSourceExtension) к кнопке в StackLayout, используя BindableLayout.ItemsSource в приложении Xamarin.Forms.

Не рабочая версия MyControl.xaml:

<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
      ...
      x:Class="MyApp.Views.MyControl">

    <StackLayout Orientation="Horizontal" 
                 BindableLayout.ItemsSource="{Binding Data}">
        <Button Command="{Binding Source={RelativeSource AncestorType={x:Type views:MyControl}}, Path=BindingContext.RemoveCommand}"
                CommandParameter="{Binding}" 
                Text="Remove"/>
    </StackLayout>

</Grid>

К сожалению, кнопка «неактивна» и не позволяет «щелкать». Такая же привязка команды отлично работает в ListView с ItemsSource.

Рабочая версия MyControl.xaml:

<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
      ...
      x:Class="MyApp.Views.MyControl">

    <ListView ItemsSource="{Binding Data}"
              HasUnevenRows="true">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Button Command="{Binding Source={RelativeSource AncestorType={x:Type views:MyControl}}, Path=BindingContext.RemoveCommand}"
                            CommandParameter="{Binding}" 
                            Text="Remove"/>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

Использование элемента управления в MainPage.xmal:

<Grid BindingContext="{Binding Cart}">
    ...
    <views:MyControl/>
    ....
</Grid>

Я вообще что-то делаю не так? Или есть хитрость, чтобы заставить это работать?

...