0

I have a telerik RadTreeListView which is populated by a C# DataTable class. All i want is to refresh the RadTreeListView UI when items are removed or added to the DataTable (i dont care about when item properties change for now).

I have looked into and tried using the ObservableCollection class, when i remove or add items to the dataTable, i can see from the debugger that the dt object has changed but the the problem is that the RadTreeListView does not update. It still shows the same old data even when i call its Rebind method.

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;
        }
    }
}

The RadTreeView's UI is supposed to update after i call the code below from my MainPage.xaml.cs

 NestedTreeView objNestedTreeView = new NestedTreeView();
 objNestedTreeView.radTreeListView.ItemsSource = Test.GetWarehouseData((DataTable)SessionApp.SessionManager.Session["myDataTable"]);
StackTrace
  • 9,190
  • 36
  • 114
  • 202
  • check the awnser here (http://stackoverflow.com/questions/8555729/observablecollection-not-updating-view) – LudwigGhislain Nov 21 '13 at 07:36
  • And where are you calling 'GetWarehouseData'? And are you sure you have binding set up correctly? And you should add telerik tag, because it is probably related. – Euphoric Nov 21 '13 at 07:37
  • Use snoop to check if the bindings are setup correctly. – Sandeep Singh Rawat Nov 21 '13 at 07:41
  • Your edit made me "what?" pretty hard. You don't have problem with DataBinding. You have problem with understanding basic OOP. How do you expect UI to update, when you are doing operations on completely unrelated object instance? – Euphoric Nov 21 '13 at 07:48

1 Answers1

0

Please consider changing the following declaration: public class Test : ObservableCollection, the type of the child element should not be like the collection type itself. Another thing, Remove the line: public new ObservableCollection Items { get; set; }

Regarding making your code work, try this (comments embedded in the code):

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;
    }

}

Now about the binding lines: you are creating each time a new UI object and bind to it, this is not the one that shown on your GUI...

Rami Yampolsky
  • 465
  • 4
  • 12