单击每个 ListViewItem 的 Button 时修改 TextBlock 值
modify the TextBlock value when click on Button of each ListViewItem
我的 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 >
我有 2 个 ListviewItems,每个都有一个 "BtnStar" Button,每个 Button 都有一个 "liketext" TextBlock,其中一个 TextBlocks 只能工作,例如,当我点击 ListViewItem1 的 btnStar 时,它会修改ListViewItem2的TextBlock的TextBlock值,当我点击ListViewItem1的BtnStar时,我无法修改ListViewItem1的TextBlock的Text,这是我的代码:
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));
}
}
}
那么,当我点击每个 ListViewItem 的 BtnStar 按钮时,如何修改 TextBlock 的值
感谢帮助
嗯。首先,您需要在 xaml 个应用程序中使用绑定方法。
您的 class 当地人实施了 INotifyPropertyChanged 但实施不当。
请检查此示例:
public string someProperty {get;set;}
public string SomeProperty
{
get
{
return someProperty;
}
set
{
someProperty =value;
NotifyPropertyChanged("SomeProperty");
}
}
在你的文本块中你有 Text={Binding SomeProperty}
你需要添加 Mode= TwoWay
文本={绑定某些属性,模式=双向}
终于在您的点击方法中
btnStar_Click
你需要做这样的事情:
var btn = sender as Button;
var local= btn.DataContext as Local;
local.SomeProperty= "my new value"
如果您在模型中正确实施了 INotifyPropertyChanged,您将在 UI 中看到更改。
就这些了。
如果对您有用,请标记此答案!
此致。
我的 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 >
我有 2 个 ListviewItems,每个都有一个 "BtnStar" Button,每个 Button 都有一个 "liketext" TextBlock,其中一个 TextBlocks 只能工作,例如,当我点击 ListViewItem1 的 btnStar 时,它会修改ListViewItem2的TextBlock的TextBlock值,当我点击ListViewItem1的BtnStar时,我无法修改ListViewItem1的TextBlock的Text,这是我的代码:
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));
}
}
}
那么,当我点击每个 ListViewItem 的 BtnStar 按钮时,如何修改 TextBlock 的值 感谢帮助
嗯。首先,您需要在 xaml 个应用程序中使用绑定方法。
您的 class 当地人实施了 INotifyPropertyChanged 但实施不当。 请检查此示例:
public string someProperty {get;set;}
public string SomeProperty
{
get
{
return someProperty;
}
set
{
someProperty =value;
NotifyPropertyChanged("SomeProperty");
}
}
在你的文本块中你有 Text={Binding SomeProperty}
你需要添加 Mode= TwoWay
文本={绑定某些属性,模式=双向}
终于在您的点击方法中 btnStar_Click
你需要做这样的事情:
var btn = sender as Button;
var local= btn.DataContext as Local;
local.SomeProperty= "my new value"
如果您在模型中正确实施了 INotifyPropertyChanged,您将在 UI 中看到更改。
就这些了。
如果对您有用,请标记此答案!
此致。