question

Dave010 avatar image
0 Votes"
Dave010 asked PmellaCastilloDelCanto-4045 commented

How do we pass the `BindingContext` of a `DataTemplate` as a `CommandParameter` when parent has a separate `BindingContext`?

How do we pass the BindingContext of a DataTemplate as a CommandParameter when parent has a separate BindingContext?

Normally I just do CommandParameter={Binding .} but because the parent view has it's own BindingContext it passes that context through. Hopefully this makes sense.


Example XAML code:

 <DataTemplate x:Key="dataTemplateRoot" x:DataType="models:ThoughtEntryContainer">
     <StackLayout
         x:Name="NTStackLayout"
         HeightRequest="{Binding MaxHeight}">
             <Editor
                     Text="{Binding thought}">
                     <Editor.Behaviors>
                             <xct:EventToCommandBehavior Command="{Binding Path=BindingContext.SetHeightsCommand, Source={x:Reference rootView}}" EventName="SizeChanged">
                                     <xct:EventToCommandBehavior.CommandParameter>
                                             <MultiBinding Converter="{StaticResource MultiConverter}">
                                                     <Binding Source="{x:Static models:ThoughtEntryType.NegativeThought}" />
                                                     <Binding Source="{Binding .}" /> <!-- This is where I want to pass dataTemplateRoot's BindingContext -->
                                                     <Binding Source="{x:Reference NTStackLayout}" />
                                             </MultiBinding>
                                     </xct:EventToCommandBehavior.CommandParameter>
                             </xct:EventToCommandBehavior>
                     </Editor.Behaviors>
             </Editor>
     </StackLayout>
 </DataTemplate>


dotnet-xamarin
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can try to use Relative Bindings like this code: <Binding Source="{Binding Source={RelativeSource AncestorType={x:Type models:ThoughtEntryContainer}}}" />


0 Votes 0 ·

Thank you for the reply. For some reason that doesn't work. I get the error System.InvalidCastException: 'Specified cast is not valid'. When I set a breakpoint and view the local varbiales that comes up as {Xamarin.Forms.Binding}, even when I cast it to ThoughtEntryContainer

0 Votes 0 ·

Could you share a demo about it? That I can reproduce this issue.

0 Votes 0 ·
Show more comments

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered PmellaCastilloDelCanto-4045 commented

Hello,​

Welcome to our Microsoft Q&A platform!

Please change: <Binding Source="{Binding Source={RelativeSource AncestorType={x:Type models:ThoughtEntryContainer}}}" /> to <Binding />.

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT
Please, and where would the command be? Could you put an example of the code with Command?

0 Votes 0 ·