Search code examples
wpfdata-bindingentity-framework-6fody-propertychanged

Using propertychanged.fody with WPF databinding and EF 6.0 database first


Can someone please advise me how I can get PropertyChanged.Fody to work with WPF databinding and EF 6.0 Database first?

I am not using MVVM.

I have an EF generated class called "Test" as follows....

///------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace RMS_EF6
{
    using System;
    using System.Collections.ObjectModel;

    public partial class Test
    {
        public int TestIdentityKey { get; set; }
        public Nullable<int> TestNumber1 { get; set; }
        public Nullable<int> TestNumber2 { get; set; }
        public string TestDescription1 { get; set; }
    }
}

I have created the following PropertyChanged.Fody class:

using PropertyChanged;
[AddINotifyPropertyChangedInterfaceAttribute]
public partial class Test
{
    private void ArithmeticTest()
    {
        TestNumber1 = 2 * TestNumber2;
        Console.WriteLine($"TestNumber1 = {TestNumber1} \n + TestNumber2 = {TestNumber2}");
    }
}

I can update the individual fields in the database with manual input through the WPF GUI OK using WPF databinding.

However, I want to execute methods like ArithmeticTest() in the code-behind when the users enter data through the GUI. Eg when the value of TestNumber2 changes as a result of user input.

Can anyone please tell me what I need to add to my code to make this happen?


Solution

  • You could add a method to your class

    public void OnTestNumber2Changed()
    {
        ArithmeticTest();
    }
    

    This will be called by Fody every time the TestNumber2 property changes as explained here.