How to access components of the view that are inside a ListView Xamarin Forms

1

I need to access the properties of the components created in View to set some values (example height and width), but they are within a ListView , so in .cs I can not give this. with x:Name .

I tried this way, but it did not work:

var stkEditarEndereco = this.FindByName<StackLayout>("stkEditarEndereco");

How can I solve this problem?

    
asked by anonymous 29.12.2016 / 19:19

3 answers

1

In order to access Xamarin.Forms components created in the XAML file, you must set the x: Name property, as shown below:


<ListView x:Name="DemoListView" />

To change its properties in code behind (.cs), enter the following data:



DemoListView.RowHeight = 100;


More information: link

Need some help?
I am at your disposal.

    
29.12.2016 / 19:54
0

To change component sizes you can use XAML properties such as HeightRequest and WidhRequest.

But if you need to change or do something in the code behind you can create a CustomCell For listView See here !

    
11.01.2017 / 01:24
0

Would not it be better to use the Styles concept to define this type of property? this way you can even reuse your layout.

For example, in my app I have a listview that needs a specific layout, with the style looks like this:

<Style x:Key="listGroup" TargetType="ListView">
    <Setter Property="BackgroundColor" Value="{DynamicResource Snow}" />
    <Setter Property="IsGroupingEnabled" Value="True"/>
  </Style>

In listView it looks like this:

<ListView
  Style="{DynamicResource listGroup}"
  GroupDisplayBinding="{Binding Key}"
  GroupShortNameBinding="{Binding Key}"
  ItemsSource="{Binding ListaFiltro}" SeparatorVisibility="None"
  SelectedItem="{Binding SelectedPedido}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <Grid Padding="5">
          <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="40"></ColumnDefinition>
          </Grid.ColumnDefinitions>

          <Label Grid.Row="0" Grid.Column="0" Text="{Binding Id}" LineBreakMode="TailTruncation">

          </Label>
          <Label Grid.Row="1" Grid.Column="0" Text="{Binding Parceiro.CardName}" Font="Small" TextColor="Gray" LineBreakMode="TailTruncation"></Label>

        </Grid>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

The important snippet here is Style="{DynamicResource listGroup}".

This example is in the direct listview, but depending on the control within your listview you can make a style specific to it.

    
24.01.2017 / 14:24