【发布时间】:2016-12-20 10:14:40
【问题描述】:
我遇到了以下问题
在我的 (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 也会改变
但我不知道不同的元素如何共享同一个 ref?
【问题讨论】:
-
你能用ref keyword吗?
-
首先要修复:开始遵循 .NET 命名约定。您的所有属性都应以大写字母开头。这似乎并不重要,但您确实希望在查看代码时减少任何形式的分心。
-
不知道“ref关键字”能否让不同的属性指向同一个引用?
-
行不需要知道它们是否可见。相反,您可以使用过滤后的行集合更改集合,这将仅使用过滤后的集合中的行更新视图
-
@Fabio,我设置了你的解决方案,它非常快,更新我的 BindingList 后唯一的小问题,通过这种方式:UCContentList[i].RowDetails = new BindingList
(MyLinqResult),我必须手动更新数据上下文。无论如何,非常感谢,您的解决方案是有效的。