XAML - Change Label Text inside Viewcell from code behind

SreeeeSreeee INMember ✭✭✭✭✭

Hi everybody,
I need to change a label text put into a Viewcell from codebehind but I receive an error dispayng "the name likecount does not exist in the current context".

I want to show the label value initially(like count), it is working.

  <ListView x:Name="ListView1"  RowHeight="500">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>

        <Label 
            Text="{Binding likeCount}"
            x:Name="likecount" 
            Font="14" 
            TextColor="Black"
            HorizontalOptions="Start" 
            VerticalOptions="Center"/>  

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

After like or unlike i want to change the label text(like count). For that i use the following code:

If(like){
 likecount.Text = item.likeCount + 1;
 } else{
 likecount.Text = item.likeCount - 1;
 }

Currently showing, The name likecount does not exist in the current context.

Can you show how you create the datamodel?

Please help me, Thanks in advance.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Text="{Binding likeCount}" vs likecount

  • SreeeeSreeee INMember ✭✭✭✭✭

    @AlessandroCaliaro Already i do that and it is working.
    My task is to access that label in cs file and change the text from cs. Do you know how to access the label inside a viewcell in cs file?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    You have not to change the "Text" property. You should change the value you are binding to the "Text" property. You have to use binding... Your ListView should have an ItemsSource also...

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/data-and-databinding/

  • SreeeeSreeee INMember ✭✭✭✭✭
     <Label 
            Text="{Binding likeCount}"
            x:Name="likecount" 
            Font="14" 
            TextColor="Black"
            HorizontalOptions="Start" 
            VerticalOptions="Center"/>  
    

    @AlessandroCaliaro Binding property working fine. I want that label inside viewcell in cs file.
    Normally x:Name.Text = "my text" is the syntax. But here it is not possible to access like that, Do you know how to access a label inside a viewcell?

  • SreeeeSreeee INMember ✭✭✭✭✭

    @AlessandroCaliaro I go through your link, for button it help that solution.

        Command="{Binding ClickCommand}"
        Public Command ClickCommand
        {    
            get { return new Command(() => Login_OnClick()); }
        }
    

    For me i want the label inside the viewcell in cs file. But for labels no Command property is available. Can you suggest a solution for accessing labels in cs file?

    Thanks for your quick response.

  • patrickmzpatrickmz USMember ✭✭

    Dear All,
    I need help in my case I'm trying if user click on the label Phone or Cell of a listview a popup show and gonna call the number, but i need to take the value of the label Phone or Cell (WITH NO IDEA HOW THIS WORK)
    This is my XAML:

    <ContentPage.Content>

    <ListView.RowHeight>




    </ListView.RowHeight>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ContentView Padding="5">
                            <Frame Padding="10" OutlineColor="Accent">
                                <StackLayout Orientation="Horizontal">
                                    <Image
                                        HeightRequest="75"
                                        Source="{Binding URL_PIC}"
                                        WidthRequest="75" />
                                    <StackLayout>
                                        <Label
                                            FontSize="20"
                                            Text="{Binding GymName}"
                                            VerticalOptions="StartAndExpand" />
                                        <Label
                                            FontSize="10"
                                            Text="{Binding Location}"
                                            VerticalOptions="CenterAndExpand" />
                                        <Label
                                            x:Name="lblPhone"
                                            FontSize="10"
                                            Text="{Binding Phone}"
                                            TextColor="green"
                                            VerticalOptions="CenterAndExpand">
                                            <Label.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="Phone_Tapped" />
                                            </Label.GestureRecognizers>
                                        </Label>
                                        <Label
                                            x:Name="lblCell"
                                            FontSize="10"
                                            Text="{Binding Cell}"
                                            TextColor="green"
                                            VerticalOptions="CenterAndExpand">
                                            <Label.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="Cell_Tapped" />
                                            </Label.GestureRecognizers>
                                        </Label>
    
                                    </StackLayout>
                                </StackLayout>
                            </Frame>
                        </ContentView>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

    //Please take a look on the Comment
    CodeBehind:

    public partial class GymsPage : ContentPage
    {
    private string phoneNumberTxt;
    private string cellNumberText;
    private T_Clubs _selectedClub {get; set;}
    private T_Clubs SelectedClub
    {
    get;set;
    }

        public GymsPage()
        {
            InitializeComponent();
        }
    
        private async void Phone_Tapped(object sender, EventArgs e)
        {
        //HERE I NEED HELP TO TAKE THE VALUE OF PHONE!!!!! HOW 
            phoneNumberTxt = PhonewordTranslator.ToNumber(SelectedClub.Phone);
            if (await this.DisplayAlert(
                            "Dial a Number",
                            "Would you like to call " + phoneNumberTxt + "?",
                            "Yes",
                            "No"))
            {
                var dialer = DependencyService.Get<IDialer>();
                if (dialer != null)
                {
                    await dialer.DialAsync(phoneNumberTxt);
                }
            }
        }
    
        private async void Cell_Tapped(object sender, EventArgs e)
        {
            //HERE I NEED HELP TO TAKE THE VALUE OF CELL !!!!! HOW 
              cellNumberText = PhonewordTranslator.ToNumber(SelectedClub.Cell);
            if (await this.DisplayAlert(
                            "Dial a Number",
                            "Would you like to call " + phoneNumberTxt + "?",
                            "Yes",
                            "No"))
            {
                var dialer = DependencyService.Get<IDialer>();
                if (dialer != null)
                {
                    await dialer.DialAsync(phoneNumberTxt);
                }
            }
        }
    
        private void Mylist_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var details = e.Item as T_Clubs;
    
    
        }
    
    }
    

    }

    ++++++++++++++++
    Thanks and regards,

  • patrickmzpatrickmz USMember ✭✭

    Dear All,
    I need help in my case I'm trying if user click on the label Phone or Cell of a listview a popup show and gonna call the number, but i need to take the value of the label Phone or Cell (WITH NO IDEA HOW THIS WORK)
    This is my XAML:

    <ContentPage.Content>

    <ListView.RowHeight>




    </ListView.RowHeight>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ContentView Padding="5">
                            <Frame Padding="10" OutlineColor="Accent">
                                <StackLayout Orientation="Horizontal">
                                    <Image
                                        HeightRequest="75"
                                        Source="{Binding URL_PIC}"
                                        WidthRequest="75" />
                                    <StackLayout>
                                        <Label
                                            FontSize="20"
                                            Text="{Binding GymName}"
                                            VerticalOptions="StartAndExpand" />
                                        <Label
                                            FontSize="10"
                                            Text="{Binding Location}"
                                            VerticalOptions="CenterAndExpand" />
                                        <Label
                                            x:Name="lblPhone"
                                            FontSize="10"
                                            Text="{Binding Phone}"
                                            TextColor="green"
                                            VerticalOptions="CenterAndExpand">
                                            <Label.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="Phone_Tapped" />
                                            </Label.GestureRecognizers>
                                        </Label>
                                        <Label
                                            x:Name="lblCell"
                                            FontSize="10"
                                            Text="{Binding Cell}"
                                            TextColor="green"
                                            VerticalOptions="CenterAndExpand">
                                            <Label.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="Cell_Tapped" />
                                            </Label.GestureRecognizers>
                                        </Label>
    
                                    </StackLayout>
                                </StackLayout>
                            </Frame>
                        </ContentView>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

    //Please take a look on the Comment
    CodeBehind:

    public partial class GymsPage : ContentPage
    {
    private string phoneNumberTxt;
    private string cellNumberText;
    private T_Clubs _selectedClub {get; set;}
    private T_Clubs SelectedClub
    {
    get;set;
    }

        public GymsPage()
        {
            InitializeComponent();
        }
    
        private async void Phone_Tapped(object sender, EventArgs e)
        {
        //HERE I NEED HELP TO TAKE THE VALUE OF PHONE!!!!! HOW 
            phoneNumberTxt = PhonewordTranslator.ToNumber(SelectedClub.Phone);
            if (await this.DisplayAlert(
                            "Dial a Number",
                            "Would you like to call " + phoneNumberTxt + "?",
                            "Yes",
                            "No"))
            {
                var dialer = DependencyService.Get<IDialer>();
                if (dialer != null)
                {
                    await dialer.DialAsync(phoneNumberTxt);
                }
            }
        }
    
        private async void Cell_Tapped(object sender, EventArgs e)
        {
            //HERE I NEED HELP TO TAKE THE VALUE OF CELL !!!!! HOW 
              cellNumberText = PhonewordTranslator.ToNumber(SelectedClub.Cell);
            if (await this.DisplayAlert(
                            "Dial a Number",
                            "Would you like to call " + phoneNumberTxt + "?",
                            "Yes",
                            "No"))
            {
                var dialer = DependencyService.Get<IDialer>();
                if (dialer != null)
                {
                    await dialer.DialAsync(phoneNumberTxt);
                }
            }
        }
    
        private void Mylist_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var details = e.Item as T_Clubs;
    
    
        }
    
    }
    

    }

    ++++++++++++++++
    Thanks and regards,

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    @patrickmz Please start a new Thread for your question. It has nothing to do with label text and you will get better responses if your thread is properly titled!
  • patrickmzpatrickmz USMember ✭✭

    @JamesLavery said:
    @patrickmz Please start a new Thread for your question. It has nothing to do with label text and you will get better responses if your thread is properly titled!

    I'm so sorry, but can you please help me with this :smile:
    https://forums.xamarin.com/discussion/119492/stored-procedure-sql

  • Vish_TankVish_Tank Member ✭✭
    > @Sreeee said:
    > @AlessandroCaliaro I go through your link, for button it help that solution.
    >
    > Command="{Binding ClickCommand}" Public Command ClickCommand { get { return new Command(() => Login_OnClick()); } }
    >
    > For me i want the label inside the viewcell in cs file. But for labels no Command property is available. Can you suggest a solution for accessing labels in cs file?
    >
    > Thanks for your quick response.

    have you solved this?
  • CharwakaCharwaka INMember ✭✭✭✭✭

    @Vish_Tank said:
    > @Sreeee said:
    > @AlessandroCaliaro I go through your link, for button it help that solution.
    >
    > Command="{Binding ClickCommand}" Public Command ClickCommand { get { return new Command(() => Login_OnClick()); } }
    >
    > For me i want the label inside the viewcell in cs file. But for labels no Command property is available. Can you suggest a solution for accessing labels in cs file?
    >
    > Thanks for your quick response.

    have you solved this?

    var tapGestureRecognizer = new TapGestureRecognizer();
    tapGestureRecognizer.Tapped += (s, e) => {
        // handle the tap
    };
    MyLabel.GestureRecognizers.Add(tapGestureRecognizer);
    
    
    <Label Source="tapped.jpg">
        <Label.GestureRecognizers>
            <TapGestureRecognizer
                    Command="{Binding MyCommand}"
                    NumberOfTapsRequired="1" />
      </Label.GestureRecognizers>
    </Image>
    
Sign In or Register to comment.