I am Adding and Removing from ObservableCollection but the Grid is not updating
I am using this code to update my grid. It is not working. Grid is not updating. I cannot find any problem with the code. This is a Help Desk app. I am trying to update a closed ticket on the server with my grid on the client. HELP !
public class DAL_LOADTICKETS
{
private static string URL ="http://helpdesk.hujelabs.com/user.php/"+Login.user_id+"/query/";
public static void/*ObservableCollection<cTicket>*/ LoadOPENTICKETS()
{
// Execute the query using the LINQ to XML
var tickets = from c in XElement.Load(URL).Elements("query") select c;
foreach (var ticks in tickets)
{
cTicket lTicket = new cTicket
{
TicketID = ticks.Element("query_id").Value,
TicketStatus = ticks.Element("status").Value,
TicketSubject = ticks.Element("subject").Value,
TicketDescription = ticks.Element("description").Value,
TicketTags = ticks.Element("tag").Value,
TimeStamp = ticks.Element("timestamp").Value,
};
if (ticks.Element("status").Value == "open")
{
if (!Window1.OpenTicketsList.Contains(lTicket))
{
Window1.OpenTicketsList.Add(lTicket);
}
}
else if (ticks.Element("status").Value == "closed")
{
if (Window1.OpenTicketsList.Contains(lTicket))
{
Console.WriteLine("VISITED BUT NOT DELETED");
Window1.OpenTicketsList.Remove(lTicket);
}
}
}
}
<Grid Width="756" Background="White">
<ListView Name="OpenReqList" HorizontalAlignment="Stretch" VerticalAlignment="Top" ItemsSource="{Binding TicketCollection}" Width="750" PreviewMouseDoubleClick="OpenReqL开发者_StackOverflow中文版ist_PreviewMouseDoubleClick" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn Width="100" Header="Ticket ID" DisplayMemberBinding="{Binding TicketID}"/>
<GridViewColumn Width="250" Header="Ticket Subject" DisplayMemberBinding="{Binding Path=TicketSubject}"/>
<GridViewColumn Width="190" Header="Ticket Send Time" DisplayMemberBinding="{Binding Path=TimeStamp}"/>
<GridViewColumn Width="190" Header="Ticket Status" DisplayMemberBinding="{Binding Path=TicketStatus}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
public class cTicket
{
public string TicketID { get; set; }
public string TicketStatus { get; set; }
public string TicketSubject { get; set; }
public string TicketDescription { get; set; }
public string TicketTags { get; set; }
public string TicketDiscussionID { get; set; }
public string ResponseAdmin { get; set; }
public string ResponseClient { get; set; }
public string TimeStamp { get; set; }
}
It looks like your code is constructing a new Ticket and trying to remove it from the ObservableCollection. Try finding the Ticket that is already in your collection, then removing it. Alternatively, you may need to override Equals on your Ticket class so ObservableCollection knows that your new Ticket is equal to the old ticket.
精彩评论