How to access view components that are inside a Listview Xamarin Forms

Asked

Viewed 534 times

1

I need to access the properties of the components created on View to set some values (example height and width), but are within a ListView, with that, in the .cs I can’t give This.Component even with the x:Name.

I tried this way, but it didn’t work:

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

How can I solve this problem ?

3 answers

1

To access on Xamarin.Forms the components created in the XAML file, you must set the property x:Name, as the example below:


<ListView x:Name="DemoListView" />

To change its properties in code Behind (.Cs), enter the following data:



DemoListView.RowHeight = 100;


More information: https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/

Need a little help?
I am at your disposal.

  • I already did that friend, only the components that are inside Listview does not let pull by x:Name

  • What specifically do you need to change? Have the file xaml to post? Possibly the best solution would be to implement everything via code Cs.

  • I need to change the image of a button inside Listview, but it won’t let me access it even with x:Name. Know some other way ?

  • Look at this post I answered. http://answall.com/questions/175651/definir-imagem-para-bot%C3%A3o-dentro-de-listview-Xamarin-Forms/175767#175767 There is an option to create Resourcedictionary for the image, but remember that it is necessary to inform the path for each platform in the case of Xamarin.Forms. https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/application/

0

wouldn’t it be better to use the Styles concept to define this type of property ? that way you can even reuse your layout.

For example, in my app I have a listview that I need a specific layout, with the style stayed that way:

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

On listview it looked 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 section here is the Style="{Dynamicresource listGroup}" .

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

  • is that what happens is this, I created the normal listview, and inside her cells has a frame with a stacklayout inside, and inside the stacklayout an image button. This image button is getting the different size in each version of android, marshmallow, Lollipop and Kitkat. In another view, I did a check before to know what the android version is and assign the right image to the button according to this version. And that’s what I wanted to try to do in this View that contains Listview

0

To change component sizes you can use XAML properties like Heightrequest and Widhrequest.

But if there is a need to change or perform something in code Behind you can create a Customcell To listview Seehere!

  • I’m already using heightRequest and Widthrequest, but in a certain time I need these properties to be changed, and I need this to be done in code Behind. However, when I try to take control with x:Name it does not leave because it is inside Listview.

  • In this case I recommend creating a contentView . perform all the programming you need and put it in control. Put here as this your Listview I give an example more Clear

Browser other questions tagged

You are not signed in. Login or sign up in order to post.