【问题标题】:Accessing a Main Window public object (Observable Collection) from another class c# wpf从另一个类 c# wpf 访问主窗口公共对象(可观察集合)
【发布时间】:2016-12-10 03:22:23
【问题描述】:

我对@9​​87654321@ 和here 有一个类似的问题

现已编辑问题以包含更多信息

我有一个 wpf c#(编辑解决方案,其中有 3 个项目。这些项目是一个名为 CH_CustomerOutlook 的项目,并引用了第二个项目 DAL 项目,第三个是 CustomLibrary End Edit)CH_CustomerOutlook 项目的主窗口正常这是一个 DataGrid,它有一个我自己的类型的公共可观察集合,称为 Outlooks

对于 CustomerOutlook 类(编辑在 DAL 项目中并且)我已经设置了 INotifyPropertyChanged 并在 NotifyPropertyChanged 方法的句柄中我想在主窗口中访问这个 Observable 集合 Outlooks 并在类方法中使用它来更新DataGrid 上的总计

任何帮助都会非常感谢

代码是

public partial class MainWindow : Window
{
    public static ProjectSettings Settings = new ProjectSettings();
    // define new observable collection for data grid
    public static ObservableCollection<SysproDAL.CustomerOutlook> OutlookCol = new ObservableCollection<SysproDAL.CustomerOutlook>();

    public MainWindow()
    {
        InitializeComponent();
        Settings.SetFromFile(Settings);
    }

~Customer Outlook 类代码是

public  class CustomerOutlook: INotifyPropertyChanged
{
     //For CustomerOutlook static non changing propetties
   public string Status { get; set; }



    //For Return Merchandise  changing propetties
    private DateTime entrydate;
    public DateTime EntryDate { get { return this.entrydate; }
                    set
                    {
                            if(this.entrydate != value)
                            {
                                    this.entrydate = value;
                                    this.NotifyPropertyChanged("EntryDate");
                            }
                    } } 
//more properties defines etc



    public event PropertyChangedEventHandler PropertyChanged;


    public void NotifyPropertyChanged(string propName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
                    this.Status = "u";
                    if (propName == "M1")
                    { 
                        //Want to Access  public observable collection "Outlooks" from main Window
                        //The call method UpdateTotals (Outlooks);
                    }
                }
            }

 public static void UpdateTotals(ObservableCollection<CustomerOutlook> Col)
            { 
                var item = Col.FirstOrDefault(i => i.Status == "T");
                if (item != null)
                {
                    item.M1 = Col.Sum(x => x.M1);
                    item.M2 = Col.Sum(x => x.M2);
                    item.M3 = Col.Sum(x => x.M3);
                }


            }

【问题讨论】:

  • 所以当您的List 中的一个项目在其NotifyPropertyChanged 中发生更改时,您希望访问您的List

标签: c# .net wpf


【解决方案1】:

单个 CustomerOutlook 对象没有对其可能所在的父集合的引用。但由于您已在 MainWindow 类中将 ObservableCollection 字段定义为静态字段,因此您可以像这样从 CustomerOutlook 类直接访问它:

UpdateTotals(MainWindow.OutlookCol);

【讨论】:

  • 您好刚刚测试了这个并且得到 MainWindow not does not exist in current context 错误。请参阅编辑,但 CustomerOutlook 类位于 DAL 的单独项目中,而 MainWindow 位于名为 CH_CustomerOutlook 的父项目中
  • 您应该如何引用另一个项目中的某些内容,而这些内容甚至没有从定义您的类的项目中引用?这是不可能的。 DAL 中的类绝对不应该对 UI 应用程序中的某个窗口有任何依赖。这是完全错误的,是一个真正糟糕的架构的例子(或者更确切地说是完全缺乏架构)。您应该在与 MainWindow 相同的项目中定义您的类,或者重新考虑您的整个方法。
  • 好的 - 谢谢 - 这可能是因为我是这方面的初学者。我想我需要在主项目中提出一些问题,然后将集合发送到 DAL 以进行集合的总和,或者您所说的不应该在 DAL 中?
  • DAL 应该只与数据库或您正在使用的任何数据存储进行通信。您应该通过传入对集合的引用来调用 DAL 方法。但您最好从 MainWindow 或任何定义您的集合的地方执行此操作:DAL.UpdateTotals(OutlookCol);
  • 谢谢 - 我想我遇到的问题是 propertychanged 处理程序在 DAL 中,因为 class 所在的位置具有更改的属性....令人困惑!我会继续努力的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
相关资源
最近更新 更多