【问题标题】:c# share reference between collection item and propertiesc# 在集合项和属性之间共享引用
【发布时间】: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),我必须手动更新数据上下文。无论如何,非常感谢,您的解决方案是有效的。

标签: c# wpf ref


【解决方案1】:

我设置了@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);
            }
        }
    }

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

感谢@法比奥

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 2012-09-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多