1

So the question is this. I have a Main WPF form which Loads a UserControl as a Child and passes a class by reference when it instantiates the child control. I subscribe to a Property_Changed event that the reference class fires when a property in the class changes. The issue I have is that the event handler in the UserControl cant't see the reference class that was passed when the control was instantiated.

Does anyone know of a way to make this available in the event, or another method to access it?

The last class in the code block shows the object coming in by ref, and the event that is trying to access it.

    public partial class MainWindow : Window
{
   // Instantiate GLobal Data Classes for Passing to User Controls
    GlobalDataClasses.GlobalData MainGlobalData = new GlobalDataClasses.GlobalData();
    public MainWindow()
    {
        InitializeComponent();
        TestControl1 TC1 = new TestControl1(MainGlobalData); // create child with ref object         
        TC1.ParentControl = this.MainWindowDock; 
        MainWindowDock.Children.Clear();
        MainWindowDock.Children.Add(TC1);
    }
}

public class GlobalData : INotifyPropertyChanged
{

    private int _GD_MyInt;
    public int GD_MyInt
    {
        get { return _GD_MyInt; }
        set { _GD_MyInt = value; OnPropertyChanged("GD_MyInt"); }
    }

    // -- PROPERTY CHANGE EVENT --
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

public partial class TestControl1 : UserControl
{
   public DockPanel ParentControl { get; set; }        

    public TestControl1(ref GlobalDataClasses.GlobalData GD) // Global Data Passed Here
    {
        InitializeComponent();

        // Subscribe to GLobal Reference Memory Notification Change EVents
        GD.PropertyChanged += GlobalData_PropertyChanged;
    }

    // -- GlobalData Property_Changed Event Handler
    private void GlobalData_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        int LocalInt;
        switch (e.PropertyName)        // switch variable and handle
        {
            case "GD_MyInt":
                LocaInt = GD.GD_MyInt  // *** reference passed object not available
                break;
        }

    }
Ric
  • 19
  • 2
  • 2
    Pretty sure you can cast `sender` to the instance of the object you need, i.e. `LocalInt = ((GlobalData)sender).GD_MyInt;` – John Wu May 19 '21 at 15:52
  • 4
    is this just "cast `sender` to `GlobalDataClasses.GlobalData`" ? btw; the `ref` here doesn't achieve anything useful – Marc Gravell May 19 '21 at 15:53
  • https://stackoverflow.com/questions/47723876/how-to-capture-old-value-and-new-value-in-inotifypropertychanged-implementation or https://stackoverflow.com/questions/7677854/notifypropertychanged-event-where-event-args-contain-the-old-value/7742890 – Trevor May 19 '21 at 15:55
  • 1
    @Daniel that's just not true; `ref` can matter just as much for reference-types (just: it only really applies when you *reassign the reference itself*) – Marc Gravell May 19 '21 at 16:36
  • 1
    The code you posted is not valid. It will fail to compile, because it does not use the `ref` keyword that's required when invoking the `TestControl1` constructor. Beyond that, there does not appear to be any need to use `ref` here anyway. You seem confused about the distinction between passing any argument "by reference", and passing a reference-type argument "by value". ... – Peter Duniho May 19 '21 at 17:45
  • ... If you want access to the object passed as the `GD` parameter to the constructor, then as long as the event is implemented correctly, you can just cast the `sender` object passed to your handler: `((GlobalDataClasses.GlobalData)sender).GD_MyInt;`. See duplicate for more details about the `sender` parameter in event handlers. – Peter Duniho May 19 '21 at 17:45
  • Thanks all, I cast the sender object and this worked. I have also created an instance of the GlobalData class at a local level in the UserControl and set it to GD (which was passed in when the UserCOntrol was instantiated). this essentially copied the reference to the globaldata class held by the parent form and has allowed me to access the data from the child elsewhere. THanks also for the clarification on the 'ref' keyword, misused in my code examples – Ric May 20 '21 at 08:38

0 Answers0