我在列表视图中有一个按钮,当我单击该按钮时,数据模板应将值加 1。我只想更新特定的行,但是当我单击按钮时,所有行都会更新。有人能帮我吗?尝试了许多解决方案,但它根本无法工作。这些是我的 xaml 代码
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="1048" Height="158">
<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0.971"/>
<GradientStop Color="#FF929990" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background><Button x:Name="btnVote" BorderBrush="Black" BorderThickness="2" Height="45" Margin="0,70,0,43" Width="78" Content="Vote" Background="Black" Click="btnVote_Click" />
<Image HorizontalAlignment="Left" Height="111" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="118" Source="{Binding Img}"/>
<HyperlinkButton Tag="{Binding UserID}" FontSize="20" ScrollViewer.VerticalScrollBarVisibility="Visible" Content="{Binding Name}" Width="918" Height="48" Margin="0,12,0,98" Foreground="Black" Tapped="HyperlinkButton_Tapped_1" HorizontalContentAlignment="Left"/>
<TextBlock FontSize="18" ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding Comment}" Width="884" Height="56" Margin="-930,59,0,43" Foreground="Black"/>
<TextBlock ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding DateTime}" Width="583" Height="36" Margin="-1230, 90, 0,0" Foreground="Black"/>
<TextBlock ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding Votes}" Margin="-500,100,0,10" Width="321" Height="48" Foreground="Black"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<TextBlock Height="81" TextWrapping="Wrap" Text="TextBlock" Width="273"/>
</ListView>
我的按钮的 xaml.cs 代码
private async void btnVote_Click(object sender, RoutedEventArgs e)
{
MyLearningStory.Clear();
addVotes();
retrieveLearningStory();
//Service1Client updateclient = new Service1Client();
//string update = await updateclient.updateVotesAsync(userId);
}
以及 getlearningstory() 的方法
public async void retrieveLearningStory()
{
btnShowMore.IsEnabled = true;
Service1Client retrieveClient = new Service1Client();
if (firstRun == true)
{
if(userId!=""){/*
learningStoryComment.AddRange(await retrieveClient.retrieveLearingStoryCommentAsync(userId, storyId));
learningStoryDatetime.AddRange(await retrieveClient.retrieveLearingStoryDatetimeAsync(userId, storyId));
learningStoryImg.AddRange(await retrieveClient.retrieveLearingStoryImgAsync(userId, storyId));
*/
learningStoryComment.AddRange(await retrieveClient.retrieveLearingStoryCommentByStoryIDAsync(storyId));
learningStoryDatetime.AddRange(await retrieveClient.retrieveLearingStoryDateTimeByStoryIDAsync(storyId));
learningStoryImg.AddRange(await retrieveClient.retrieveLearingStoryImgByStoryIDAsync(storyId));
learningStoryUserId.AddRange(await retrieveClient.retrieveLearingStoryUserIDByStoryIDAsync(storyId));
}
}
await Task.Delay(1000);
string name = "";
Service1Client client1 = new Service1Client();
if(userId==null){
userId = roamingSettings.Values["userID"].ToString();
}
if (firstRun == true)
{
initialValue = 0;
firstRun = false;
currentUserId = userId;
}
//finding the author of the learningstory
for (int a = initialValue; a < learningStoryComment.Count; a++)
{
if (a == (learningStoryComment.Count-1))
{
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
writer.WriteBytes(learningStoryImg[a]);
await writer.StoreAsync();
// Create bitmap image
BitmapImage b = new BitmapImage();
b.SetSource(randomAccessStream);
name = await retrieveClient.retrieveNameByUserIDAsync(learningStoryUserId[0]);
// status = await retrieveClient.RetrieveUserStatusAsync(learningStoryUserId[0]);
imgFirstPost.Source = b;
txtFirstPostComment.Text = learningStoryComment[a];
txtFirstPostDateTime.Text = learningStoryDatetime[a].ToString();
hbtnName.Content = name;
hbtnName.Tag = currentUserId;
}
}
//finding the other user that posted in the learningstory
// for (int a = learningStoryComment.Count - 1; a > -1; a--)
//tbvotesdisplay = count.ToString();
for (int a = initialValue; a < learningStoryComment.Count; a++)
{
count++;
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
writer.WriteBytes(learningStoryImg[a]);
await writer.StoreAsync();
// Create bitmap image
BitmapImage b = new BitmapImage();
b.SetSource(randomAccessStream);
name = await client1.retrieveNameByUserIDAsync(learningStoryUserId[a]);
//vote = await client1.retrieveVotesAsync(learningStoryUserId[a]);
//status = await retrieveClient.RetrieveUserStatusAsync(learningStoryUserId[a]);
if (a != (learningStoryComment.Count - 1))
{
MyLearningStory.Add(new LearningStoryItem(learningStoryComment[a], name, learningStoryDatetime[a], b, 1, userId, votescount));
}
lvLearningStory.DataContext = MyLearningStory;
我的加票方式
public async void addVotes()
{
votescount++;
}
我的 listview.datacontext 类
public class LearningStoryItem
{
public BitmapImage Img { get; set; }
public string Comment{ get; set; }
public DateTime DateTime { get; set; }
public string Name {get; set;}
public int StoryID { get; set; }
public string UserID { get; set; }
public string LastCommented { get; set; }
public string View { get; set; }
public double Votes { get; set; }
public LearningStoryItem(double votes)
{
Votes = votes;
}
public LearningStoryItem(string comment, DateTime datetime, BitmapImage img)
{
Comment = comment;
DateTime = datetime;
Img = img;
Votes = 200;
}
public LearningStoryItem(string comment)
{
Comment = comment;
}
public LearningStoryItem(int storyid)
{
StoryID = storyid;
}
public LearningStoryItem(string comment, string name, DateTime datetime, BitmapImage img)
{
Comment = comment;
DateTime = datetime;
Img = img;
Name = name;
Votes = 200;
}
public LearningStoryItem(string comment, string name, DateTime datetime, BitmapImage img, int storyid)
{
Comment = comment;
DateTime = datetime;
Img = img;
Name = name;
StoryID = storyid;
Votes = 200;
}
public LearningStoryItem(string comment, string name, DateTime datetime, BitmapImage img, int storyid, string userid,double votes)
{
Comment = comment;
DateTime = datetime;
Img = img;
Name = name;
StoryID = storyid;
UserID = userid;
Votes = votes;
}
public LearningStoryItem(string comment, string name, DateTime datetime, BitmapImage img, int storyid, string userid,string lastCommented)
{
Comment = comment;
DateTime = datetime;
Img = img;
Name = name;
StoryID = storyid;
UserID = userid;
LastCommented = lastCommented;
Votes = 200;
}
public LearningStoryItem(string comment, string name, DateTime datetime, BitmapImage img, int storyid, string userid, string lastCommented,string view)
{
Comment = comment;
DateTime = datetime;
Img = img;
Name = name;
StoryID = storyid;
UserID = userid;
LastCommented = lastCommented;
View = view;
Votes = 200;
}
public LearningStoryItem(DateTime datetime)
{
DateTime = datetime;
Votes = 200;
}
public LearningStoryItem(BitmapImage img)
{
Img = img;
Votes = 200;
}
public LearningStoryItem(int storyid,string userid)
{
StoryID = storyid;
UserID = userid;
Votes = 200;
}
}
}