How can I access a specific collectionview child? In this case, the label “DataCadastroLabel”

2021-01-27T16:24:12.817+00:00

I want to change the value of VerticalOptions.

I can't directly access this label, even if I pass its name DataCadastroLabel.

These layout changes are only to adapt some components to the device's landscape mode

XAML

<CollectionView x:Name="ItemsListView"
                                ItemsSource="{Binding Notificacoes}"
                                SelectionMode="None"
                                ItemsUpdatingScrollMode="KeepItemsInView"
                                VerticalScrollBarVisibility="Never"
                                HorizontalScrollBarVisibility="Never"
                                IsGrouped="True">
                    <CollectionView.GroupHeaderTemplate>
                        <DataTemplate x:DataType="model:NotificacaoGrupoData">
                            <Label Text="{Binding Data_agrupada}"
                                   FontSize="20"
                                   FontAttributes="Bold"
                                   HorizontalTextAlignment="Center"
                                   Padding="0,15,0,15"/>
                        </DataTemplate>
                    </CollectionView.GroupHeaderTemplate>

                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <StackLayout x:DataType="model:Notificacao">
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="logo"
                                           HeightRequest="40"
                                           Margin="0,0,5,0"
                                           VerticalOptions="CenterAndExpand"/>

                                    <StackLayout VerticalOptions="CenterAndExpand">
                                        <StackLayout Orientation="Horizontal">
                                            <Label Text="{Binding app_nome}" 
                                                   LineBreakMode="NoWrap" 
                                                   Style="{DynamicResource ListItemTextStyle}" 
                                                   FontSize="20" 
                                                   FontAttributes="Bold"
                                                   VerticalTextAlignment="Center"
                                                   HorizontalOptions="StartAndExpand"/>

                                            <Label Text="{Binding dt_cadastro_hora}"
                                                   x:Name="DataCadastroLabel"
                                                   LineBreakMode="NoWrap" 
                                                   Style="{DynamicResource ListItemTextStyle}" 
                                                   FontSize="13"
                                                   FontAttributes="Bold"
                                                   VerticalOptions="CenterAndExpand"
                                                   Margin="0,0,20,0"/>
                                        </StackLayout>

                                        <Label Text="{Binding mensagem}"
                                               TextType="Html"
                                               WidthRequest="290"
                                               LineBreakMode="TailTruncation"/>
                                    </StackLayout>

                                    <Label Text="&#xf105;"
                                           FontFamily="FontAwesomeBold"
                                           TextColor="#ACACAC"
                                           FontSize="24"
                                           HorizontalOptions="EndAndExpand"
                                           VerticalOptions="CenterAndExpand">
                                        <Label.GestureRecognizers>
                                            <TapGestureRecognizer
                                                NumberOfTapsRequired="1"
                                                Command="{Binding Source={RelativeSource AncestorType={x:Type local:NotificacoesViewModel}},
                                                          Path=NotificacaoSelecionada}"
                                                CommandParameter="{Binding .}">
                                            </TapGestureRecognizer>
                                        </Label.GestureRecognizers>
                                    </Label>
                                </StackLayout>

                                <BoxView HeightRequest="1"
                                         BackgroundColor="#DDDDDD"
                                         Margin="0,5,0,5"/>
                            </StackLayout>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

C#

ItemsListView.DataCadastroLabel.VerticalOptions
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,997 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Joe Manke 1,091 Reputation points
    2021-01-27T18:50:29.51+00:00

    Define the view for the ItemTemplate in its own class and do it in that code-behind. Using x:Name on an inline DataTemplate doesn't make sense, because you are presumably creating several instances of the view.

    0 comments No comments

  2. JessieZhang-MSFT 7,711 Reputation points Microsoft Vendor
    2021-01-28T10:16:59.763+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    You can try to create a Xamarin.Forms DataTemplateSelector to achieve this.

    You can add a field(e.g. Status) to your binded item of CollectionView.

    public class ItemModel  
    {  
        // other field  
    
        public int Status { get; set; }  // Landscape and portrait  
    }  
    

    And create DataTemplateSelector like this:

    public  class MyDataTemplateSelector:DataTemplateSelector  
    {  
    
          public DataTemplate LandTemplate { get; set; }  
    
      public DataTemplate PortraitTemplate { get; set; }  
    
       protected override DataTemplate OnSelectTemplate(object item, BindableObject container)  
        {  
        return ((ItemModel)item).Status == 0 ? LandTemplate : PortraitTemplate;  
       }  
    }  
    

    When you switch between landscape and portrait, you can change the value of status in your ItemModel.

    The program will select different templates( LandTemplate and PortraitTemplate) based on the value of status.

    For more details, you can refer to the official sample : https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/templates-datatemplateselector/

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.