Answered by:
How to access controls of the xaml that are inside from a ListView ?

Question
-
User282494 posted
I need to declare values to the properties to the controls that build my view in code behind (.cs), but this controls are inside from a ListView and i don't know access.
I already tried:
var lblLogradouro= this.FindByName<Label>("lblLogradouro");
But the Compiling don't know find this control.
My xaml:
<ListView x:Name="lvEnderecos" RowHeight="210"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Grid x:Name="GridControl" RowSpacing="0" ColumnSpacing="0"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="20"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> </Grid.ColumnDefinitions> <StackLayout x:Name="stkEnderecos" BackgroundColor="White" Padding="0, 0, 0, 0" Grid.Row="0"> <Label x:Name="lblNomeDestinatario" Text=" {Binding EndDestinatario}" Font="16" TextColor="Black"></Label> <Label x:Name="lblTipoEndereco" Text=" {Binding EndTipo, StringFormat='({0})'}" Font="16" TextColor="Black"></Label> <Label Text=" _________________________________________________ " TextColor="Black"></Label> <Grid x:Name="GridControl2" RowSpacing="0" ColumnSpacing="0"> <Grid.RowDefinitions> <RowDefinition Height="25"/> <RowDefinition Height="25"/> <RowDefinition Height="25"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Label x:Name="lblLogradouro" Font="14" TextColor="Black" Text="{Binding EndLogradouro}" Grid.Row="0" Grid.Column="0"></Label> <Label x:Name="lblNumero" Font="14" TextColor="Black" Text="{Binding EndNumero}" Grid.Row="0" Grid.Column="1"></Label> <Label x:Name="lblComplemento" Font="14" TextColor="Black" Text="{Binding EndComplemento}" Grid.Row="0" Grid.Column="2"></Label> <Label x:Name="lblBairro" Font="14" TextColor="Black" Text="{Binding EndBairro}" Grid.Row="1" Grid.Column="0"></Label> <Label x:Name="lblCidade" Font="14" TextColor="Black" Text="{Binding EndCidade}" Grid.Row="1" Grid.Column="1"></Label> <Label x:Name="lblUF" Font="14" TextColor="Black" Text="{Binding EndUF}" Grid.Row="1" Grid.Column="2"></Label> <Label x:Name="lblCEP" Font="14" TextColor="Black" Text="{Binding EndCep, StringFormat='CEP: {0}'}" Grid.Row="2" Grid.ColumnSpan="2"></Label> </Grid> <Grid x:Name="GridControl3" RowSpacing="0" ColumnSpacing="5"> <Grid.RowDefinitions> <RowDefinition Height="25"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <StackLayout x:Name="stkEditarEndereco" Grid.Row="0" Grid.Column="0" Padding="5, 0, 0, 0" HeightRequest="25" WidthRequest="25"> <Button x:Name="btnEditarEndereco" BackgroundColor="Transparent" Image="editar.png" HeightRequest="25" WidthRequest="25" Command="{Binding Path=BindingContext.EditarEnderecoCommand, Source={x:Reference MeusEnderecosView}}" CommandParameter="{Binding .}" /> </StackLayout> <StackLayout x:Name="stkExcluirEndereco" Grid.Row="0" Grid.Column="1" Padding="5, 0, 0, 0" HeightRequest="25" WidthRequest="25"> <Button x:Name="btnExcluirEndereco" BackgroundColor="Transparent" Image="excluir1.png" HeightRequest="25" WidthRequest="25" Command="{Binding Path=BindingContext.ExcluirEnderecoCommand, Source={x:Reference MeusEnderecosView}}" CommandParameter="{Binding .}" /> </StackLayout> </Grid> </StackLayout> </Grid> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
How to i can do it ?
Monday, January 2, 2017 4:08 PM
Answers
-
User180523 posted
@Andreeh_Araujo said: I need to declare values to the properties to the controls that build my view in code behind (.cs),
Stop right there. That is already a problem. You shouldn't be trying to do that, at all. Instead read up on
DataTemplate
. Its a way of defining how theListViewItem
should be presented: How you assign all those properties in the XAML: Not in code behind.Next... Before you go any further you REALLY need to learn about
Styles
and not have 500 duplicate pieces of XAML for font size, text color and so on. All of that repetitive definition should be in aStyle
that you can assign one time in theListView
ResourceDictionary
or in yourApplication.Dictionary
. https://redpillxamarin.wordpress.com/2016/12/23/203-now-for-the-left-foot/I would urge you to just stop for a bit. Take the time to learn more about XAML. Kind of a "Work smarter not harder" approach. If you took 20 hours to learn more about XAML you would save twice that on the first couple pages of this application alone; and have far cleaner markup.
- Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
Monday, January 2, 2017 10:12 PM