[英]Xamarin Button Command (inside of ListView.ItemTemplate) Not Firing
[英]Button Command inside Listview not firing
我有一个显示 ObservableCollection 的 ListView。 Listview的每一项都有一个按钮和一个带有“AggiungiCommand”绑定的Command和一个CommandParameter。 当我单击按钮时,该命令不会触发。 这是代码:
<ContentPage xmlns: …
x:Name="MieiAcquistiViewPage"
...
<StackLayout CompressedLayout.IsHeadless="True">
<StackLayout.BindingContext>
<local:MieiAcquistiViewModel/>
</StackLayout.BindingContext>
<ListView x:Name="listView"
ItemsSource="{Binding AcquistiList}"
ItemTapped="listView_ItemTapped"
CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame Style="{StaticResource frameListView}" >
<StackLayout Margin="-15">
<Label Text="{Binding Articolo.Descrizione}" />
<StackLayout Orientation="Horizontal"
HorizontalOptions="Fill" VerticalOptions="CenterAndExpand" >
<StackLayout Orientation="Horizontal"
HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand" >
<Label Margin="10,0,0,0"
Text="{Binding Prezzo, StringFormat='{}{0:€ 0.00}'}"
HorizontalOptions="StartAndExpand" />
</StackLayout>
<StackLayout Orientation="Horizontal"
HorizontalOptions="End" VerticalOptions="Center">
<Label Text="{Binding QtaEdit}"
TextColor="Black"
VerticalTextAlignment="Center" VerticalOptions="FillAndExpand"/>
<Button x:Name="addButton"
VerticalOptions="Center"
HorizontalOptions="Center"
Text="+"
Command="{Binding AggiungiCommand, Source={x:Reference MieiAcquistiViewPage}}"
CommandParameter="{Binding .}"/>
</StackLayout>
</StackLayout>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
...
</ContentPage/>
在我看来 model class:
public MieiAcquistiViewModel()
{
AggiungiCommand = new Command<RigaStoricoModel>(AggiungiArticolo);
}
public void AggiungiArticolo(RigaStoricoModel prodotto)
{
// some stuff
}
public ICommand AggiungiCommand { private set; get; }
Command="{Binding MieiAcquistiViewModel.AggiungiCommand, Source={x:Reference MieiAcquistiViewPage}}
更改为Command="{Binding BindingContext.AggiungiCommand, Source={x:Reference MieiAcquistiViewPage}}
。 请注意我如何用BindingContext.
替换MieiAcquistiViewModel
。 .
假设您正在引用该页面,并且您想要访问支持该页面的视图 model 中的命令。
如果您已经附加了视图模型,则无需在绑定中为 AggiungiCommand 加上“MieiAcquistiViewModel”前缀。 你可以这样做:
Command="{Binding Source={x:Reference MieiAcquistiViewPage}, Path=BindingContext.AggiungiCommand,Mode=TwoWay}" CommandParameter="{Binding .}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.