imthath
imthath

Reputation: 1688

Modify Grid created using XAML in code behind - Xamarin.Forms

I'm creating a cross platform Xamarin.Forms app. In a page, I've the following list view.

<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <Grid x:Name="activityGrid">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Label Text="{Binding Name}" />
            </Grid>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate> 

Here as you can see I have one row and two columns for each item in the list view. the first label I've defined will be set in Row=0 and Column=0, as that is the default. Now with respect to Row=0 and Column =1, I want it to be a Switch or Entry based on some criteria. Here's my code behind.

foreach (var activity in activities)
{
    if (activity.Measurement == 0)
    {
        Switch yesNo = new Switch();
    }
    else
    {
        Entry measure = new Entry();
    }
}

Now help me add the children to the Grid Layout based on the requirement.

Upvotes: 1

Views: 1986

Answers (1)

Jason
Jason

Reputation: 89102

To add a control to a grid programatically, do this:

activityGrid.Children.Add(control, col, row);

Upvotes: 1

Related Questions