Alan2
Alan2

Reputation: 24572

Is there a way I can pass a grid name or a parameter into a tapped event?

I have this code and somehow I want to get the text value of the first label to the tapped event in the CS code. As the tapped event is on the grid I had an idea of putting the text value into x:Name.

<ViewCell >
   <Grid VerticalOptions="CenterAndExpand"  x:Name="{Binding [0].Name}" Tapped="atiSelectValueX" >
      <local:StyledLabel Text="{Binding [0].Name}" HorizontalOptions="StartAndExpand" />
      <local:StyledLabel IsVisible="{Binding [0].IsSelected}" TextColor="Gray" HorizontalOptions="End" Text="x" />
   </Grid>
</ViewCell>

The CS code I have so far:

    void atiSelectValueX(object sender, EventArgs e)
    {
        var cell = sender as Grid;
        if (cell == null)
            return;

        var selected = cell. <<< I want to get the name here

What I would like to do is to get the x:Name value in the CS code. I was hoping to get the sender information but it seems like I cannot enter

cell.Name

Is there another way I can get a parameter like the name (which is the same as the text in the first label always) in the C# code?

Upvotes: 0

Views: 207

Answers (1)

Jason
Jason

Reputation: 89102

The x:Name is metadata used by the XAML tools, not an actual property of the object. You could also bind Name to an unused property, like StyleID, and access that instead

<Grid VerticalOptions="CenterAndExpand" StyleId="{Binding [0].Name}" ...

and then

var selected = cell.StyleId;

Upvotes: 1

Related Questions