How to give name to any element inside CollectionView in Maui .Net

Ahmed Thabet Elshamandy 35 Reputation points
2023-08-30T12:36:36.1266667+00:00

Hello,

I need to give name to any element in CollectionView template to use it in code behind.

I want to use this element and give it more actions like shadow .

I need to control this shadow on platform which I will use.

I tried to use x:Name but it not working . and give me error say

"Error CS0103 The name 'testborder' does not exist in the current context"

Thanks,

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,988 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,726 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,406 Reputation points Microsoft Vendor
    2023-08-31T06:06:21.33+00:00

    Hello,

    I tried to use x:Name but it not working . and give me error say"Error CS0103 The name 'testborder' does not exist in the current context"`

    Because elements will be re-used in Datatemple of Collectionview, you can not get elements by x:Name.

    But you can use data-binding and mvvm to implement it.

    I need to control this shadow on platform which I will use.

    For example, I want to add the shadow for Image control like following code. I bind the two properties for Image Shadow called ShaodowColor and ShadowRadius

      <CollectionView ItemsSource="{Binding Monkeys}">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Grid Padding="10">
                         
                                <Image 
                                       Source="{Binding ImageUrl}"
                                       Aspect="AspectFill"
                                       HeightRequest="60"
                                       WidthRequest="60" >
                                    <Image.Shadow>
                                        <Shadow Brush="{Binding ShaodowColor}"
                                                Offset="20,20"
                                                Radius="{Binding ShadowRadius}"
                                                Opacity="0.8" />    
                                    </Image.Shadow>
    
                               </Image>
                               
                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
    

    Then you can add these property in your Model.

    public class Monkey
    {
        
        public string ImageUrl { get; set; }
        public string ShaodowColor { get; set; }
        public int ShadowRadius { get; set; }
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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 additional answers

Sort by: Most helpful

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.