question

lht-0429 avatar image
0 Votes"
lht-0429 asked LeonLu-MSFT answered

Data binding command parameter programmatically

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'

dotnet-xamarin
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.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered

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.


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.