【发布时间】:2017-07-15 15:06:45
【问题描述】:
我正在构建一个 Xamarin Forms 应用程序并尝试从 DataTemplate 引用父控件的绑定上下文:
<flv:FlowListView Grid.Row="3" x:Name="abc"
Grid.Column="0"
Grid.ColumnSpan="2"
FlowColumnCount="3"
SeparatorVisibility="None"
HasUnevenRows="true"
FlowItemsSource="{Binding ProductPackageBatch}"
FlowLastTappedItem="{Binding LastTappedItem}"
FlowItemTappedCommand="{Binding ItemTappedCommand}">
<flv:FlowListView.FlowColumnTemplate>
<DataTemplate>
<StackLayout Margin="5, 10, 10, 5">
<StackLayout.Children>
<Frame Padding="2"
OutlineColor="{DynamicResource ProductGridImageBorderColor}"
HasShadow="False"
BackgroundColor="{DynamicResource ProductGridBackgroundColor}">
<Image Aspect="AspectFit"
WidthRequest="100"
Source="{Binding ImageBase64, Converter={StaticResource Base64ToImageConverter}}">
<Image.HeightRequest>
<OnPlatform x:TypeArguments="x:Double"
iOS="100"
Android="150" />
</Image.HeightRequest>
</Image>
</Frame>
<Label Style="{StaticResource ProductGridName}"
Text="{Binding Name}" />
<Label Style="{StaticResource ProductGridFormattedPrice}"
Text="{Binding FormattedPrice}"
FontAttributes="Bold" />
<Button BackgroundColor="{DynamicResource BuyButtonColor}"
TextColor="{DynamicResource TextColor}"
BorderRadius="0"
Margin="5, 0, 5, 0"
VerticalOptions="End"
Text="Buy"
Command="{Binding Source={x:Reference abc}, Path=BindingContext.AddToUserCartCommand}"
CommandParameter="{Binding .}" />
</StackLayout.Children>
</StackLayout>
</DataTemplate>
</flv:FlowListView.FlowColumnTemplate>
</flv:FlowListView>
有一个调用 AddToUserCartCommand 的事件,我想从 FlowListView 中的按钮调用该事件。我尝试了几种不同的方法来引用控件,但该方法永远不会被调用。以下按钮位于此控件之外并且可以正常工作:
<Button Grid.Row="4"
Margin="30"
BackgroundColor="{DynamicResource BuyButtonColor}"
TextColor="{DynamicResource TextColor}"
BorderRadius="0"
Text="Add to Cart"
Command="{Binding AddToUserCartCommand}"
CommandParameter="{Binding ProductPackage.Id}" />
谁能帮忙解释一下我做错了什么?
【问题讨论】:
-
感谢您的评论 - 我之前没有读过那篇文章,但是它确实引导我找到了解决方案 - 事实证明,除了引用页面而不是 ListView,我也通过了命令参数的类型错误。
标签: binding xamarin.forms