2017-02-24 90 views
0

我建立一個Xamarin窗體應用程序,我試圖從引用一個DataTemplate父控件的綁定上下文:Xamarin表單綁定到家長控制

<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}" /> 

任何人都可以幫助解釋我在做什麼錯?

+2

[如何ListView控件按鈕的綁定上下文設置於母公司的Xamarin形式結合上下文(HTTP的可能重複://計算器。 com/questions/40371459/how-to-set-binding-context-of-listview-button-to-the-binding-context-of-parent-i) –

+0

感謝您的評論 - 我之前沒有閱讀過那篇文章,但它確實將我引向解決方案 - 結果是,除了引用頁面而不是ListView外,我還將錯誤的類型作爲Command參數傳遞。 – markpirvine

回答

0

的命令集和CommandParamter正確綁定上下文:

<Button BackgroundColor="{DynamicResource BuyButtonColor}" 
             BindingContext="{x:Reference Name=abc}" 
             TextColor="{DynamicResource TextColor}" 
             BorderRadius="0" 
             Margin="5, 0, 5, 0" 
             VerticalOptions="End" 
             Text="Buy" 
             Command="{Binding Path=BindingContext.AddToUserCartCommand}" 
             CommandParameter="{Binding Path=BindingContext.ProductPackage.Id}" />