0

I have a simmalar question to these to items here and here

Question now edited to include further info

I have a wpf c# (EDIT solution which has 3 projects in. The projects are a project call CH_CustomerOutlook and references the second project DAL project, the third is a CustomLibrary End Edit) The CH_CustomerOutlook project has the main window as normal on this is a DataGrid with a public observatable collection of my own type called named Outlooks

For the Class CustomerOutlook (Edit is in the DAL project and ) I have set up INotifyPropertyChanged and in the handle for the NotifyPropertyChanged method I want to access this Observable collection Outlooks in the main window and use it in a class method to update the totals on the DataGrid

Any help would be great thanks

The code is

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

~The Customer outlook class code is

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


            }
Community
  • 1
  • 1
Ian W
  • 385
  • 2
  • 10
  • 30
  • So you want to have access your `List` when an item, which is part of your `List`, is changed in its `NotifyPropertyChanged`? – TripleEEE Dec 09 '16 at 14:09

1 Answers1

0

A single CustomerOutlook object has no reference to the parent collection it may be in. But since you have defined the ObservableCollection field as static in your MainWindow class you can access it directly from the CustomerOutlook class like this:

UpdateTotals(MainWindow.OutlookCol);
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Hi just tested this and got MainWindow not does not exist in current context error. Please see Edit but CustomerOutlook Class is in separate project for DAL and MainWindow is in parent project called CH_CustomerOutlook – Ian W Dec 09 '16 at 19:17
  • How are you supposed to be able to reference something in another project that is not even referenced from the project where your class is defined? This is not possible. And a class in a DAL should definetely not have any dependency upon some window in the UI application. This is just totally wrong and an example of a truly awful architecture (or rather a lack of an architecture altogether). You should either define your class in the same project as the MainWindow or reconsider your entire approach. – mm8 Dec 09 '16 at 19:39
  • Ok - thanks - it probably because I am a beginner in this stuff. I guess I need to raise and event some how in the main project that then send the Collection to the DAL to do the sum of the collection, or is that what you say should not be in the DAL? – Ian W Dec 09 '16 at 21:09
  • A DAL should only communicate with the database or whatever data storage you are using. You should call the DAL method by passing in a reference to the collection. But you better do this from the MainWindow or whereever your collection is defined: DAL.UpdateTotals(OutlookCol); – mm8 Dec 09 '16 at 21:21
  • Thank you - I suppose the problem I have is propertychanged handler is in the DAL because that where class is that has the properties that change....it is confusing! I will keep working on it – Ian W Dec 09 '16 at 21:29