使用ObservableCollection不更新数据控件的UI

时间:2013-11-21 07:25:35

标签: c# silverlight data-binding telerik observablecollection

我有一个telerik RadTreeListView,它由C#DataTable类填充。我想要的是在删除项目或将项目添加到DataTable时刷新RadTreeListView UI(我不关心项目属性何时更改)。

我已经研究并尝试使用ObservableCollection类,当我删除或添加项目到dataTable时,我可以从调试器看到dt对象已经改变但问题是RadTreeListView没有更新。即使我调用其Rebind方法,它仍会显示相同的旧数据。

RadTreeViewList XAML

 <telerik:RadTreeListView x:Name="radTreeListView" ItemsSource="{Binding Items}" 
                                 SelectionMode="Multiple" AutoGenerateColumns="False" FontSize="11" SelectionChanged="radTreeListView_SelectionChanged">
            <telerik:RadTreeListView.ChildTableDefinitions>
                <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items}" />
            </telerik:RadTreeListView.ChildTableDefinitions>
            <telerik:RadTreeListView.Columns >
                <telerik:GridViewSelectColumn />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding StartDate}" Header="Start Date" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding EndDate}" Header="End Date" />
            </telerik:RadTreeListView.Columns>
        </telerik:RadTreeListView>

C#Class

   namespace ProjectServerDashboard
{
    public class Test : ObservableCollection<Test>
    {
        private Test(string strName, string strID, string StartDate, string EndDate, string strParentId,Test Cat_parent)
        {
            ParentId = strParentId;
            Name = strName;
            ID = strID;
            this.StartDate = StartDate;
            this.EndDate = EndDate;
            Parent = Cat_parent;
            Items = new ObservableCollection<Test>();
        }

        public string ID { get; set; }
        public string Name { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string ParentId { get; set; }
        Test Parent { get; set; }

        public new ObservableCollection<Test> Items { get; set; }

       public static ObservableCollection<Test> GetWarehouseData(DataTable dt)
        {
           var clubs = new ObservableCollection<Test>();
           Test parent = null;
           Test firstChild = null;
           Test secondChild = null;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strTaskParentUID = Convert.ToString(dt.Rows[i]["TID"]).Trim();
                string strTaskID = Convert.ToString(dt.Rows[i]["TID"]).Trim();
                string strTaskName = Convert.ToString(dt.Rows[i]["TName"]);
                string strTaskStartDate = Convert.ToString(dt.Rows[i]["TDate"]);
                string strTaskEndDate = Convert.ToString(dt.Rows[i]["TEndDate"]);
                int TaskOutlineLevel = Convert.ToInt32(dt.Rows[i]["TLevel"]);

                if (i == 0 && TaskOutlineLevel == 1)
                {
                    parent = new Test(strTaskName, strTaskID, strTaskStartDate, strTaskEndDate,strTaskParentUID, null);
                }
                else
                {
                    switch (TaskOutlineLevel)
                    {
                        case 2:
                            firstChild = new Test(strTaskName, strTaskID, strTaskStartDate, strTaskEndDate,
                                                             strTaskParentUID, parent);
                            if (parent.ID == strTaskParentUID)
                            {
                                parent.Items.Add(firstChild);
                            }
                            break;
                        case 3:
                            secondChild = new Test(strTaskName, strTaskID, strTaskStartDate, strTaskEndDate,
                                                              strTaskParentUID, firstChild);

                            if (firstChild.ID == secondChild.ParentId)
                            {
                                firstChild.Items.Add(secondChild);
                            }
                            break;
                    }
                }
            }

            clubs.Add(parent);
            SessionApp.SessionManager.Session["myDataTable"] = clubs
            return clubs;
        }
    }
}

从我的MainPage.xaml.cs调用下面的代码后,RadTreeView的UI应该会更新

 NestedTreeView objNestedTreeView = new NestedTreeView();
 objNestedTreeView.radTreeListView.ItemsSource = Test.GetWarehouseData((DataTable)SessionApp.SessionManager.Session["myDataTable"]);

1 个答案:

答案 0 :(得分:0)

请考虑更改以下声明:public class Test:ObservableCollection,子元素的类型不应该像集合类型本身一样。 另一件事,删除行:public new ObservableCollection Items {get;组; }

关于使代码正常工作,请尝试此操作(代码中嵌入的注释):

public class Test
{
    private Test(string strName, string strID, string StartDate, string EndDate, string strParentId, Test Cat_parent)
    {
        ParentId = strParentId;
        Name = strName;
        ID = strID;
        this.StartDate = StartDate;
        this.EndDate = EndDate;
        Parent = Cat_parent;
    }


    public string ID { get; set; }
    public string Name { get; set; }
    public string StartDate { get; set; }
    public string EndDate { get; set; }
    public string ParentId { get; set; }
    Test Parent { get; set; }


}



public class TestsCollection : ObservableCollection<Test>
{


    // looks llike you try to implement here a singelton:

    static TestsCollection instance = null;

    public static TestsCollection GetWarehouseData(DataTable dt)
    {
        if (instance == null)
        {
            instance = new TestsCollection();
        }


        // here add your conversion code but:
        // you must work on the same instance that you bound before that,
        // dont create a new one each time, as it will not the one that you bound to your GUI


        return instance;
    }

}

现在关于绑定线:你每次创建一个新的UI对象并绑定它时,这不是你的GUI上显示的那个......