0

I've I really weird problem with my WPF / C# application. I've got a property which returns another property. Now I make a variable and set it to one of these properties. If I now change the value by binding, the variable is also changed.

To simplify it, here's the code:

Here's the first property:

public MainDataObject CmObj_Temp { get; set; }

Which is used here:

public MainDataObject CmObj_MainData { 
  get { 
    return TemporaryDataStore.CmObj_Temp; 
  } 
  set {
    TemporaryDataStore.CmObj_Temp = value;
    this.RaisePropertyChanged(() => this.CmObj_MainData);
  }
}

From which I set a variable here:

CmObj_Backup = TemporaryDataStore.CmObj_Temp;

or also like this (makes no different):

CmObj_Backup = ((VM)this.DataContext).CmObj_MainData;

And also use for binding here:

<TextBox Text="{Binding CmObj_MainData.Str_Internnr, Mode=TwoWay}"/>

Now if I change the text of the Textbox it also changes it here:

CmObj_Backup.Str_Internnr);

Can someone tell my why?

How can I change that?

Thx


This is an smaller form of my code:

public class DataObject 
{
  public string Str_Test1 {get; set;}
  public string Str_Test2 {get; set;}
  // --> Much more properties
}

public static class TempData 
{
  public static DataObject DObj1 {get;set;}
}

public class ViewModel
{
  public DataObject DObj2 {
    get {
      return TempData.DObj1;
    }

    set {
      TempData.DataObjet.DObj1 = value;
      this.RaisePropertyChanged(() => this.DObj2);
    }
}

public partial class MainWindow : Window
{

  public MainWindow()
  {
    var VM = new ViewModel();
    this.DataContext = VM;
  }

  public void SomeWhereInTheSoftware()
  {
    ((ViewModel)this.DataContext).DObj2.Str_Test1 = "Before";
    ObjBackup = ((ViewModel)this.DataContext).DObj2;
    ((ViewModel)this.DataContext).DObj2.Str_Test1 = "After";

    // --> Here is ObjBackup.Str_Test1 also "After"!!
  }

}
Damon_Kronski
  • 17
  • 1
  • 8
  • 1
    Prolly cos `CmObj_Backup` is a reference and not a copy of `TemporaryDataStore.CmObj_Temp`. Maybe have a look at [this](http://stackoverflow.com/a/129395/1834662) for a DeepClone or implement a copy function yourself to create a copy of the object. – Viv May 05 '14 at 15:48

1 Answers1

0

If you would show full code blocks instead of randomly chosen lines of code it would be easier to follow. Your example isnt very clear to me.

However, I think you are having an issue because you think you are have 2 copies of an object when you really have 1. Objects are kept as reference so if you create a MainObject and a CopyObject you cant just set CopyObject equal to MainObject and expect to have a real copy.

Again, I could be way off given I dont understand your question fully but for example:

class A {
    public string Message { get; set; }
}

public static void Main()
{
    A mainData = new A();
    mainData.Message = "Main Data Message";

    A backupData = mainData;
    backupData.Message = "Backup Data Message";


    Console.WriteLine(mainData.Message);
    // Prints Backup Data Message

    Console.WriteLine(backupData.Message);
    // Prints Backup Data Message
}

Edit: cloning as a solution

As Viv mentioned in the comment the solution to your problem would be to clone the object, which creates an actual copy of the object as opposed to a reference to the object.

you would update class A in this way:

class A : ICloneable 
{
    public string Message { get; set; }

    public override object Clone()
    {
         A clone = new A();

         clone.Message = this.Message;

         return clone;
    }
}

reference to ICloneable is here http://msdn.microsoft.com/en-us/library/system.icloneable.aspx

german129
  • 237
  • 2
  • 10
  • Thanks for your answer. I edited my Question with all important code. I don't wanna do this manualy because there are too much properties. – Damon_Kronski May 06 '14 at 06:32