user3821206
user3821206

Reputation: 619

modify the TextBlock value when click on Button of each ListViewItem

I have this code for my ListView:

 <ListView  x:Name="listme">
 <ListView.ItemTemplate >
   <DataTemplate >
     <Grid>
       ...
      <Button Background="{Binding ButtonColor}"  x:Name="btnStar" 
Click="btnStar_Click" Tag={Binding}>
           <Image/>
          <TextBlock Text="{Binding Path=all_like}" x:Name="liketext" />
      </Button>
     </Grid>
   </DataTemplate >
 </ListView.ItemTemplate >
</ListView >

I have 2 ListviewItems,each one has a "BtnStar" Button,each Button has a "liketext" TextBlock,one of those TextBlocks works only,per example when I click on btnStar of ListViewItem1 it modifies the TextBlock value of ListViewItem2's TextBlock,I can't modify the Text of TextBlock of ListViewItem1 when I click on BtnStar of ListViewItem1,this is my code:

 ObservableCollection<Locals> Locals = new ObservableCollection<Locals>();
     public async void getListePerSearch()
    {
        try
        {
            UriString2 = "URL";
            var http = new HttpClient();
            http.MaxResponseContentBufferSize = Int32.MaxValue;
            var response = await http.GetStringAsync(UriString2);
            var rootObject1 = JsonConvert.DeserializeObject<NvBarberry.Models.RootObject>(response);

           foreach (var item in rootObject1.locals)
                {
                    Item listItem = new Item();
                    if (listItem.all_like == null)
                        {
                            listItem.all_like = "0";
                        }

                listme.ItemsSource = Locals;
   }
        private void Button_Click(object sender, RoutedEventArgs e)
                {
                    var btn = sender as Button;
                    var item = btn.Tag as Locals;
                    item.all_like = liketext.Text;
                    liketext.Text = (int.Parse(item.all_like) + 1).ToString();
                    }

Locals.cs:

public class Locals : INotifyPropertyChanged
{
    public int id_local { get; set; }
    public string all_like { get; set; }


    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }
}

so,how can I modify the value of the TextBlock when I click on the BtnStar Button of each ListViewItem thanks for help

Upvotes: 0

Views: 44

Answers (1)

RicardoPons
RicardoPons

Reputation: 1353

Well. first of all you need use Binding approach in xaml apps.

your class Locals implement INotifyPropertyChanged but is bad implemented. Please check this example:

public string someProperty {get;set;}
public string SomeProperty 

{

get
 {
   return someProperty;
 }
 set
 {
   someProperty =value;
   NotifyPropertyChanged("SomeProperty");
 }
}

in your textblock you have Text={Binding SomeProperty}

you need to add Mode= TwoWay

Text={Binding SomeProperty, Mode= TwoWay}

finally in your click method btnStar_Click

you need to do something like this:

var btn = sender as Button;
var local= btn.DataContext as Local;
local.SomeProperty= "my new value"

If you have implemented correctly INotifyPropertyChanged in your model you will see the change in your UI.

that's all.

Please mark this answer if it's useful for you!

Best Regards.

Upvotes: 1

Related Questions