【问题标题】:Using ObservableCollection not updating UI of data control使用 ObservableCollection 不更新数据控件的 UI
【发布时间】:2013-12-05 13:30:04
【问题描述】:

我有一个由 C# DataTable 类填充的 Telerik RadTreeListView。我想要的只是在项目被删除或添加到 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#类

   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"]);

【问题讨论】:

  • 在这里查看遮阳篷 (stackoverflow.com/questions/8555729/…)
  • 你在哪里调用“GetWarehouseData”?你确定你的绑定设置正确吗?你应该添加telerik标签,因为它可能是相关的。
  • 使用 snoop 检查绑定是否设置正确。
  • 你的编辑让我“什么?”挺难的。 DataBinding 没有问题。您在理解基本 OOP 方面存在问题。当您对完全不相关的对象实例进行操作时,您希望 UI 如何更新?

标签: c# silverlight data-binding telerik observablecollection


【解决方案1】:

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

关于使您的代码工作,试试这个(嵌入在代码中的cmets):

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 上显示的那个...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2019-03-16
    • 2014-12-15
    • 2015-11-17
    • 2017-05-24
    • 2016-11-05
    • 1970-01-01
    相关资源
    最近更新 更多