c#在集合项和属性之间共享引用

时间:2016-12-20 10:14:40

标签: c# wpf ref

我面临以下问题

在我的(C#/ WPF)应用程序中 illustration

我构建了一个itemsControl,它显示了一个UserControl集合,其中包含一个dataGrid。 我使用以下代码结构:

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

必须能够过滤所有dataGrid行。要做到这一点,我使用AllRowsDetails.IsRowVisible来触发每行的崩溃/可见属性。

问题是,如果我为每个UserControl数据网格的每一行设置AllRowsDetails.IsRowVisible,则计算需要花费太多时间。

所以,我计算一次ICContent.AllRowsVisibility, 我希望ICContent.AllRowsVisibility元素和AllRowsDetails.IsRowVisible元素共享相同的引用。

以这种方式: UCContentList [i] .rowDetails [j] .IsRowVisible = AllRowsVisibility [j]

每次AllRowsVisibility [j]改变,UCContentList [i] .RowDetails [j] .IsRowVisible也改变

但我不知道不同的元素如何共享同一个参考?

1 个答案:

答案 0 :(得分:0)

我设置了@Fabio解决方案:

我构建了描述我的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; }
    }

我实现了两个ICContent对象

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

在我的codeBehind中,我为更改的选定过滤器事件添加了一个新的事件处理程序:

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

效果很好,而且速度非常快。

谢谢@Fabio