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

Dave 61 Reputation points
2021-08-08T17:37:55.65+00:00

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>
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,406 Reputation points Microsoft Vendor
    2021-08-13T09:51:10.507+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.