我正在向ObservableCollection添加和删除,但网格没有更新

本文关键字:网格 更新 删除 ObservableCollection 添加 | 更新日期: 2023-09-27 17:59:09

我正在使用此代码更新我的网格。它不起作用。网格未更新。我找不到代码有任何问题。这是一个帮助台应用程序。我正在尝试用客户端上的网格更新服务器上已关闭的票证。救命!

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="OpenReqList_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; }
    }

我正在向ObservableCollection添加和删除,但网格没有更新

看起来您的代码正在构建一个新Ticket,并试图从ObservableCollection中删除It。尝试查找集合中已存在的Ticket,然后将其删除。或者,您可能需要覆盖Ticket类上的Equals,以便ObservableCollection知道您的新Ticket等于旧Ticket。