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