我有這樣的代碼爲我的ListView按鈕:修改TextBlock的值當點擊的每一個ListViewItem
<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 >
我有2個ListviewItems,每個人心中都有一個「BtnStar」按鈕,每個按鈕都有一個「liketext」 TextBlock,其中一個TextBlocks僅適用於每個示例,當我點擊ListViewItem1的btnStar時,它修改了ListViewItem2的TextBlock的TextBlock值,當我點擊ListViewItem1的BtnStar時,我無法修改ListViewItem1的TextBlock文本,這是我的代碼:
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));
}
}
}
所以,我怎麼能修改TextBlock的價值時,我在每一個ListViewItem 感謝的BtnStar按鈕單擊幫助
任何幫助請:( – user3821206