c# share reference between collection item and properties

126 views Asked by At

i'm facing the following issue

in my (C#/WPF) application illustration

i build an itemsControl that display a collection of UserControl which themselves contain, among other, a dataGrid. i'm using the following code structure:

 // define the content of ItemsControl
public class ICContent
{
    public BindingList<bool> AllRowsVisibility { get; set; }
    public BindingList<UCContent> UCContentList { get; set; }
}

// define the content of 1 UserControl
public class UCContent
{
    public string GeneralStuff { get; set; }
    public BindingList<AllRowsDetails> RowDetails { get; set; }

}

// define all the datagrid rows for each UserControl
public class AllRowsDetails
{
    // IsRowVisible is use in the dataBinding to collapse/display the row
    public bool IsRowVisible { get; set; }
    public string ColumnA { get; set; }
    public string ColumnB { get; set; }
}

all the dataGrid rows must be able to be filtered. to do that i use AllRowsDetails.IsRowVisible to trigger the collapse/visible property for each rows.

the problem is that if i set AllRowsDetails.IsRowVisible for each rows of each UserControl datagrid, the calculation take too much time.

So, i calculate ICContent.AllRowsVisibility one time, and i want the ICContent.AllRowsVisibility elements and AllRowsDetails.IsRowVisible elements share the same reference.

in this way: UCContentList[i].rowDetails[j].IsRowVisible = AllRowsVisibility[j]

and every time AllRowsVisibility[j] change, UCContentList[i].RowDetails[j].IsRowVisible change too

but i don't know how different elements can share the same ref?

1

There are 1 answers

0
m2c On

I set up the @Fabio solution:

i build the class that describe my ItemsControl

   // define the content of ItemsControl
    public class ICContent
    {
        public BindingList<UCContent> UCContentList { get; set; }
    }

    // define the content of 1 UserControl
    public class UCContent
    {
        public string GeneralStuff { get; set; }
        public BindingList<AllRowsDetails> RowDetails { get; set; }
    }

    // define all the datagrid rows for each UserControl
    public class AllRowsDetails
    {
        public string CodeClient { get; set; }
        public string columnA { get; set; }
        public string columnB { get; set; }
    }

i instanciate two ICContent objects

public class Init
{
    public ICContent AllUCContentList { get; set; }
    public ICContent ToDisplayUCContentList { get; set; }

    public Init()
    {
        // original object 
        AllUCContentList = new ICContent();
        // object dedicated to the display
        ToDisplayUCContentList = new ICContent();
    }
}

And in my codeBehind i add a new event handler to the changing selected filter event:

    private void changeDisplayToSelectedClient(object sender, ListChangedEventArgs e)
    {
        Mouse.SetCursor(Cursors.Wait);

        if (MainWindow.theSelectedClient[0] != null)
        {
            for (int uc = 1; uc < ToDisplayUCContentList.Count; uc++)
            {
                List<AllRowsDetails> theFilteredList = (from item in AllUCContentList[uc].RowDetails
                                       where item.codeClient == MainWindow.theSelectedClient[0].Code
                                       select item).ToList();

                ToDisplayUCContentList[uc].RowDetails = new BindingList<AllRowsDetails>(theFilteredList);
            }
        }
    }

it works well, and it's pretty fast.

Thanks @Fabio