Teim0
Teim0

Reputation: 9

DataContext overrides assigned public variable

I am currently working with .NET 6 WPF and got some strange behaviour using custom classes and assigning as DataContext.

My code looks like this:

public partial class EditImage : Window
{
    public bool closed = false;

    customclass defaultvalue = new customclass();
    public EditImage(customclass inputvalue)
    {
        InitializeComponent();

        // assign Datacontext
        this.DataContext = inputvalue;
        defaultvalue = inputvalue;
    }

    // checking CheckBox with Binding to Office variable and clicking save-button


    private void btn_Save_Click(object sender, RoutedEventArgs e)
    {
        var editedvalue = this.DataContext as customclass;

        // If changes made in Window, something should be executed
        if ((defaultvalue.Office != editedvalue.Office)
        {
            // Never reaches this section, because defaultvalue contains always the same data as   
            // editedvalue
        }
    }
}

Whereas customclass and its child class expandedclass looks like this:

public partial class customclass 
{
   public int ID {get; set;} 
   
   public string Articlenumber {get, set;}

   public bool Office {get; set;}
}

internal class expandedclass : customclass
{
   public DateTime Created {get; set;}

   public bool Deprecated {get; set;} 
}

As described in the first code section, I never reach the lines inside the if-statement, because both variables contain the same values. The input to the EditImage-class is of type expandedclass, not customclass. So even when I assign editedvalue as customclass, it is always of type expandedclass...

What can I do to assign the input to EditImage to a variable, which will not be changed through user input inside the Window? So that I can compare the default input with the changed?

What I tried: Changed the order of the assignments of the variables, set defaultvalue to static, set defaultvalue to readonly, cast all variables as customclass

Edit: My working solution using AutoMapper now looks like this:

using AutoMapper;

customclass defaultvalue;
public EditImage(customclass inputvalue)
{
    InitializeComponent();

    defaultvalue = inputvalue;

    MapperConfiguration config = new MapperConfiguration(cfg => cfg.CreateMap<customclass, customclass>());
    Mapper mapper = new Mapper(config);

    var changablevalue = new customclass();

    changablevalue = mapper.Map<customclass>(inputvalue);

    // assign Datacontext
    this.DataContext = changablevalue;

}

Upvotes: -1

Views: 63

Answers (1)

mm8
mm8

Reputation: 169150

Set the DataContext to a separate instance of the customclass:

readonly customclass defaultvalue;
public EditImage(customclass inputvalue)
{
    InitializeComponent();
    defaultvalue = inputvalue
    DataContext = new customclass();
}

You will then be able to edit this instance in the view using the data bindings while defaultvalue stays unchanged.

Upvotes: -1

Related Questions