Reputation: 192
I have 18 entries in grid layout. Values of entries are same type so I want to store in a list using view model. How can I make property of list that can store all values of entries in list when button is clicked?
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<control:BorderlessEntry Grid.Row="0" Grid.Column="0" Placeholder="1-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="0" Grid.Column="1" Placeholder="2-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="1" Grid.Column="0" Placeholder="3-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="1" Grid.Column="1" Placeholder="4-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="2" Grid.Column="0" Placeholder="5-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="2" Grid.Column="1" Placeholder="6-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="3" Grid.Column="0" Placeholder="7-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="3" Grid.Column="1" Placeholder="8-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="4" Grid.Column="0" Placeholder="9-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="4" Grid.Column="1" Placeholder="10-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="5" Grid.Column="0" Placeholder="11-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="5" Grid.Column="1" Placeholder="12-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="6" Grid.Column="0" Placeholder="13-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="6" Grid.Column="1" Placeholder="14-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="7" Grid.Column="0" Placeholder="15-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="7" Grid.Column="1" Placeholder="16-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="8" Grid.Column="0" Placeholder="17-Tag No" Style="{StaticResource EntryStyle}"/>
<control:BorderlessEntry Grid.Row="8" Grid.Column="1" Placeholder="18-Tag No" Style="{StaticResource EntryStyle}"/>
</Grid>
<buttons:SfButton
Command="{Binding SignUpCommand}"
CornerRadius="10"
Margin="{core:OnPlatformOrientationThickness PhonePortrait='20,32',
PhoneLandscape='150,32',
TabletPortrait='200,50',
TabletLandscape='300,50',
Desktop='30'}"
FontFamily="{StaticResource Montserrat-SemiBold}"
Style="{StaticResource SfButtonStyle}"
Text="Save"
Clicked="SfButton_Clicked"/>
I know how to bind the properties and using commands to do the task but how to get all these entries values in list? I can't get all the values one by one by creating 18 properties and then pushing in list. Please tell me better way. I'm new and need your help.
Another Question
How can I make the grid Dynamic so I can generate specific number of entries however I want?
Upvotes: 0
Views: 103
Reputation: 1013
i can't get all the values one by one by creating 18 properties and then pushing in list.please tell me better way
Try to traverse the Children of the grid and detect if the item is type of Entry
(in your code should be 'BorderlessEntry').
private void Button_Clicked(object sender, EventArgs e)
{
foreach (var item in grid.Children)
{
if (item.GetType().Equals(typeof(Entry)))
{
//handle the data
}
}
}
How can I make the grid Dynamic so I can generate specific number of entries however I want?
To add items to grid dynamically, you could achieve this in code behind.
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(60, GridUnitType.Absolute) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(100, GridUnitType.Absolute) });
grid.Children.Add(new Entry(),left_value,top_value);
Update:
To get the entries in the mvvm pattern, try to use MessagingCenter. Because you didn't set binding to the 'BorderlessEntry', the value of the entries could not be obtained directly in view model. The MessagingCenter could help you to call the function code of the page in the model class.
Upvotes: 1