Howto get and set Picker in Datagrid

Hi

I've a following celltemplate:

<sync:GridTemplateColumn HeaderText="Title"
                         MappingName="Tol"
                         Width="130"
                         >
<sync:GridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Entry Grid.Column="0"
       Grid.Row="0"
       Text="{Binding Tol}"
       FontSize="Small"
       x:Name="tol_e"
       IsVisible="{Binding TolEditable}"/>
<Picker Grid.Column="0"
        Grid.Row="0"
        x:Name="picker_tol"
        Title="Choose ......"
        IsVisible="{Binding TolEditable, Converter= {StaticResource inverter}}"
        SelectedIndexChanged="PickerTol_SelectedIndexChanged"
        ItemsSource="{Binding Tolok}"/>
</Grid>
</DataTemplate>
</sync:GridTemplateColumn.CellTemplate>
</sync:GridTemplateColumn>

The picker's items are different by rows. I'dont want to use picker's SelectedItem property. I would like to set picker's SelectedIndex from ContentPage. How to can access to picker_tol picker from code to do this.

Thanks, 

 Laszlo


3 Replies

KK Karthikraja Kalaimani Syncfusion Team September 24, 2021 07:11 AM UTC

Hi Laszlo, 

Thank you for contacting Syncfusion support. 

We can access the picker in code behind by writing the behavior class for the Grid. Please refer to the below code snippet. 

Code snippet : 
 <syncfusion:GridTemplateColumn HeaderText="Title" MappingName="No_1" Width="130">
                        <syncfusion:GridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid>
                                    <Grid.Behaviors>
                                        <local:CustomBehavior></local:CustomBehavior>
                                    </Grid.Behaviors>
                                        <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                        <RowDefinition Height="*"></RowDefinition>
                                        </Grid.RowDefinitions>
                                    <Entry Grid.Column="0" Grid.Row="0" FontSize="Small" x:Name="tol_e">

                                    </Entry>
                                    <Picker Grid.Column="0" Grid.Row="0" x:Name="picker_tol"
                                            BindingContext="{x:Reference viewModel}"
                                           
                                           SelectedIndexChanged="picker_tol_SelectedIndexChanged"
                                            ItemsSource="{Binding CustomerNames}"></Picker>
                                </Grid>
                                </DataTemplate>
                            </syncfusion:GridTemplateColumn.CellTemplate>
                    </syncfusion:GridTemplateColumn>
...
 public class CustomBehavior : Behavior<Grid>
    {
        protected override void OnAttachedTo(Grid bindable)
        {
            base.OnAttachedTo(bindable);
            (bindable.FindByName("picker_tol") as Picker).SelectedIndex = 3;

        }
    }


Please refer to the below documentation to know about behavior : 
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/behaviors/


Regards,
Karthik Raja



SL Szántai László September 27, 2021 11:53 AM UTC

Thanks, but I'd like to acces to the picker via name e,g. picker_tolXXX where the XXX is a rowindex.



KK Karthikraja Kalaimani Syncfusion Team September 28, 2021 12:54 PM UTC

Hi Laszlo, 

Thanks for the update. Please let us know when you want to access the picker based on the row index because the Picker is loaded in DataTemplate views. We cannot access it directly from code behind.

Regards,
Karthik Raja 



Loader.
Up arrow icon