0

I have a ListView in Xamarin forms which has a Viewcell in it. The Viewcell contains an lable control with a x:Name attribute. Now I tried to x:name in code behind but it is showing compile time error(The x:name does not exists in current context). The below code im using.

<ListView x:Name="UnPaidInvoicesList"
 SeparatorVisibility="None" IsPullToRefreshEnabled="True"
                      HasUnevenRows="True"                    
                       ItemSelected="UnPaidInvoicesList_ItemSelected" 
                       VerticalOptions="FillAndExpand">
                     <ListView.ItemTemplate>
                        <DataTemplate>
                           <ViewCell >
                    <StackLayout x:Name="stkDuedate" Orientation="Horizontal">
                        <Label x:Name="lblDueDate1" Text="Due Date:" TextColor="Gray" />
                             <Label x:Name="lblDueDate"  TextColor="{StaticResource Pink}" />
                                                     </StackLayout>
   
                             </ViewCell>
                         </DataTemplate>
                     </ListView.ItemTemplate>
                 </ListView>```

Im new to this xamarin forms. Please help on this.
neha
  • 65
  • 8
  • 2
    this has been discussed a million times. Elements inside a template cannot be referenced by name. You need to use data binding – Jason May 26 '22 at 14:05
  • Look here for the documentation https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding#data-binding And the example https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/workingwithlistview/ . – Bas H May 26 '22 at 15:00
  • Does this answer your question? [How do I access a control inside a XAML DataTemplate?](https://stackoverflow.com/questions/16375375/how-do-i-access-a-control-inside-a-xaml-datatemplate) – ToolmakerSteve May 26 '22 at 19:44

1 Answers1

0

What features do you want to implement? Just as Jason said,elements inside a template cannot be referenced by name.

If you want to disply a data list , you can bind the data to a ListView.

For this, you can refer to the following code:

     <ListView  x:Name="lstView" RowHeight="60"  ItemsSource="{Binding veggies}" HasUnevenRows="True" IsPullToRefreshEnabled="False" SelectionMode="None">
        <ListView.ItemTemplate >
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" BackgroundColor="Olive">
                        <StackLayout Orientation="Vertical">
                            <Label Text = "{Binding Name}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/>
                            <Label Text = "{Binding Type}" AbsoluteLayout.LayoutBounds="50, 35, 200, 25"/>
                        </StackLayout>
                        <Image Source="{Binding Image}" HorizontalOptions="End" AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50 "/>
                    </StackLayout>
                </ViewCell>

            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

For more,you can check document Xamarin.Forms ListView and

Customizing ListView Cell Appearance.

You can also check the sample Xamarin.Forms - ListView Sample: Custom Cells.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19