Share via

Data binding command parameter programmatically

l_h_t 21 Reputation points
2021-04-06T08:45:18.05+00:00

Consider this xaml.

        <CollectionView
            x:Name="layerColView"
            x:FieldModifier="public"
            SelectionMode="Single"
            ItemsSource="{Binding Items}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout
                        Padding="5"
                        x:DataType="viewModels:LayerViewModel"
                        >
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer
                                Command="{Binding BindingContext.ItemTapped, Source={x:Reference layerColView}}"
                                CommandParameter="{Binding}"></TapGestureRecognizer>
                            <DropGestureRecognizer
                                AllowDrop="True"
                                DragLeaveCommand="{Binding BindingContext.ItemDragLeave, Source={x:Reference layerColView}}"
                                DragLeaveCommandParameter="{Binding}"
                                DragOverCommand="{Binding BindingContext.ItemDraggedOver, Source={x:Reference layerColView}}"
                                DragOverCommandParameter="{Binding}"
                                Drop="DropGestureRecognizer_Drop_Collection"
                                DropCommand="{Binding BindingContext.ItemDropped, Source={x:Reference layerColView}}"
                                DropCommandParameter="{Binding}" />
                        </StackLayout.GestureRecognizers>

                        <StackLayout Orientation="Horizontal"
                                     HorizontalOptions="FillAndExpand"
                                     Spacing="5">
                            <StackLayout.GestureRecognizers>
                                <DragGestureRecognizer
                                    CanDrag="True"
                                    DragStarting="DragGestureRecognizer_DragStarting_Collection"
                                    DragStartingCommand="{Binding BindingContext.ItemDragged, Source={x:Reference layerColView}}"
                                    DragStartingCommandParameter="{Binding}" />
                            </StackLayout.GestureRecognizers>
                            <Image Source="{Binding IconSource}"
                                   WidthRequest="30"
                                   HorizontalOptions="StartAndExpand"></Image>
                            <Label
                                FontSize="14"
                                HeightRequest="40"
                                TextColor="Black"
                                HorizontalTextAlignment="Center"
                                Text="{Binding LayerName}"
                                VerticalTextAlignment="Center"
                                HorizontalOptions="StartAndExpand">

                            </Label>
                        </StackLayout>
                        <Grid
                            BackgroundColor="LightYellow"
                            HeightRequest="50"
                            IsVisible="{Binding IsBeingDraggedOver}" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

Now as you can see when i write DragStartingCommandParameter="{Binding}" DragStartingCommandParameter recognized as the binded class for this item template. My question is how can i do this in c# code?

 return new DataTemplate(() =>
            {
               ...

                DropGestureRecognizer dropGR = new DropGestureRecognizer();
                dropGR.AllowDrop = true;

                dropGR.SetBinding(DropGestureRecognizer.DragLeaveCommandParameterProperty, new Binding("", source: this));
...
            });
        }

This doesnt work. Gives the error 'Error: 'path' cannot be an empty string'

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-04-06T11:19:29.737+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Based on your code, I find a similar thread:

    https://stackoverflow.com/questions/35237556/set-xamarin-forms-binding-commandparameter-in-code-behind

    Then I change your code like following format. You can reference it.

       Binding objBinding1 = new Binding()  
               {  
                   Source = layerColView.BindingContext  
               };  
                dropGR.SetBinding(DropGestureRecognizer.DragLeaveCommandParameterProperty, objBinding1);  
    

    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 comments No comments

Your answer

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