Hi i'm experimenting with databinding and have some strange things going on here. I have a MainWindow
and a VarAssignmentWindow
. I also have a Dictionary where my Data is stored:
public Dictionary<String, MDevice> devices = new Dictionary<string,MDevice>();
In the VarAssignmentWindow
i want to change some values inside of the devices
-Object if i click on an OK button, or do not apply the changes if i hit the Cancel button.
I call the VarAssignmentWindow
this way:
VarAssignmentWindow window = new VarAssignmentWindow(devices);
window.Owner = this;
window.ShowDialog();
if (window.canceled == false)
{
//Save changes if OK was clicked
devices = window.devices;
}
as you can see i want to overwrite the MainWindow.devices
with VarAssignmentWindow.devices
if i hit the OK button, otherwhise nothing should happen.
Now here is whats going on inside of the VarAssignmentWindow
class:
public Dictionary<String, MDevice> devices = new Dictionary<string,MDevice>();
public bool canceled = true;
public VarAssignmentWindow(Dictionary<String, MDevice> devices)
{
InitializeComponent();
this.devices = devices; //This seems to be ByRef, but only, if i bind the items to the listbox
updateListBox();
}
private void updateListBox()
{
lstVars.Items.Clear();
foreach (var dev in devices)
{
foreach (var vari in dev.Value.savedVarDic)
{
lstVars.Items.Add(vari.Value);
}
}
}
private void cmdOK_Click(object sender, RoutedEventArgs e)
{
canceled = false;
this.Close();
}
private void cmdCancel_Click(object sender, RoutedEventArgs e)
{
canceled = true;
this.Close();
}
If i change anything inside the ListBox, its always changed in the MainWindow.devices
object too, no matter if i hit cancel or ok.
To be sure if its a ByRef i made the following test:
public VarAssignmentWindow(Dictionary<String, MDevice> devices)
{
InitializeComponent();
this.devices = devices;
updateListBox();
this.devices = null;
}
--> devices in MainWindow is null afterwards
public VarAssignmentWindow(Dictionary<String, MDevice> devices)
{
InitializeComponent();
this.devices = devices;
//updateListBox();
this.devices = null;
}
--> devices in MainWindow is not null (its what it was before)
Is it just a stupid DataBinding mistake i made? please help!