How to change the Translationx property of an element through x:Name?

Asked

Viewed 25 times

0

I have a XAML with the following structure:

<swipeview:SwipeCollectionView ItemsSource="{Binding Items}" x:Name="swipeListView">

  <swipeview:SwipeCollectionView.ItemTemplate>

    <DataTemplate>
      <material:MaterialCard Elevation="1" x:Name="Card" Padding="1" BackgroundColor="White" WidthRequest="{Binding Source={x:Reference swipeListView}, Path=Width}" CornerRadius="4" Margin="5,5,5,5">

        <StackLayout>

          <StackLayout Padding="15">
            <Button IsVisible="True" Text="SwipeRight" HorizontalOptions="EndAndExpand" Clicked="SwipeClick" CommandParameter="{Reference Name=loopedElement}" />


          </StackLayout>

          <swipeview:SwipeItemView x:Name="loopedElement" BoundItem="{Binding .}" ChangeOpacity="True" IsVisible="True" SwipeRightCompleted="SwipeRightCompleted" SwipeLeftCompleted="SwipeLeftCompleted">

            <!--Swipe Main Content -->
            <swipeview:SwipeItemView.MainContent>
              <StackLayout>
                <material:MaterialCard Elevation="1" Padding="2" BorderColor="Gray" CornerRadius="4" x:Name="mcard" >

                  (...)

However, mcard does not appear in code Behind.

The intention is that when the button is clicked, the Translationx property of the Materialcard element will be changed.

I can access swipeListView in code Behind but not mcard.

I’ve been stuck in this for days. I thank everyone who can help.

1 answer

0

You won’t be able to get x:name because it is inside an Itemtemplate/Datatemplate

You’ll have to pick up the mcard’s parent object and go looking for Chidren[Intel] until you find the item you want.

try something similar to this:

(VisualContainer)swipeListView.GetType().GetProperty("VisualContainer", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(swipeListView).Children[indice]...;

and from the result of that, you get Children

or with an action, Voce picks up the action object, accesses Parent and then searches for Children

 private void EnviarPara(object sender, SwitchStateChangedEventArgs e)
        {
            var switched = (SfSwitch)sender;
            StackLayout stPai = (StackLayout)switched.Parent;
            
            if (((Label)stPai.Children[0]).Text != "Todos"){
                  return true; 
            }

Browser other questions tagged

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