PHenry
PHenry

Reputation: 293

Why is my PropertyChanged event firing twice?

I'm using wpf, mvvmlight and EF. I have two properties on my entity object which are loosely linked and in my XAML, I would like to have one change when the other changes. Right now, I'm having a problem with the first property changed event firing twice.

My xaml setup is like this, I have DataGrid up top on my xaml, I have my

ItemsSource="{Binding MonthlyDonorDetails}" IsSynchronizedWithCurrentItem="True"

The textbox is like,

<TextBox Grid.Column="0" Text="{Binding BankCustomerID}"/>

Next, in my VM, I load up the data like this,

int rowID = 1;
foreach(var row in monthlyDonorsQuery)
{
    row.RowID = rowID++;
    row.PropertyChanged += new PropertyChangedEventHandler(MonthlyDonorDataRow_PropertyChanged);
}

MonthlyDonorDetails = new ObservableCollection<MonthlyDonorFundCode>(monthlyDonorsQuery);

This affords me a row id on each row to help user differentiate, and (I'm thinking) allows me to setup a handler for when individual fields change within the row. Then I set the backing collection for the grid. And this is all working just fine.

NOW, I want to be able to keep two fields in lock-step with each other, an AccountID and an CustomerID. When the user manually\typing\input changes the AccountID fields, I want to have some code to change the CustomerID, but I don't want this firing needlessly multiple times. Right now its firing twice, and I don't know why? Can anyone see my mistake please?

private void MonthlyDonorDataRow_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if(e.PropertyName == "FirstName" || e.PropertyName == "LastName")
        RaisePropertyChanged("DonorName");

    if(e.PropertyName == "AccountID")
    {
        MonthlyDonorFundCode monthlyDonation = sender as MonthlyDonorFundCode;
        int customerID = GetCimsIdFromBankCustomerID(monthlyDonation.AccountID);
        Debug.WriteLine("should be setting to " + customerID);
    }
}

When I'm debugging this, all I can see is the EF setter is getting called twice, but I don't know why cause I'm not setting that value. Any help\guidance would be appreciated. Thank you very much.

Upvotes: 1

Views: 4121

Answers (3)

EaG
EaG

Reputation: 21

I have the same problem with twice invocation of propertychanged. The same thing when I try to catch the extra invoke of the propertychanged, the call stack says it is called from the [External Code]. After I upgraded my Xamarin version from 4.0 to latest (Xamarin 5.0).

Upvotes: -1

PHenry
PHenry

Reputation: 293

I think I found out my problem. But it raises another problem for me. I'm calling the method which adds a PropertyChanged handler, twice, while loading. That's another problem I have to figure out.

Thank you Lavr for trying to help me out.

Upvotes: 2

Sergey L.
Sergey L.

Reputation: 96

  1. Put breakpoint in AccountID setter and look at stack trace then setter is called. You can find "who" updating your property twice.
  2. Also you can update AccountID setter like this:

    private int _accountId;
    
    public int AccountID
    {
        get { return this._accountId; }
        set 
        {
            if (this._accountId == value) return;
            this._accountId = value;
            this.RisePropertyChanged("AccountID");
        }
    }
    

Upvotes: 0

Related Questions