-1

I've a DataGrid with 5 columns. First 3 columns are part of parent object and last 2 columns are part of child object. I've a userName column as 6th column.

When I update anything from first 3 columns of parent object, userName reflects correctly. But when I update anything from last 2 columns of child object, it doesn't update the userName.

I've tried binding it and doing logic in child object and before 'save' userName updates correctly for last 2 columns but after save it reverts back to previous userName.

My question is, how do I keep the updated userName after save?

XAML:

<DataGridTextColumn Header="Updated By" IsReadOnly="True" Binding="{Binding ChildObject.UpdatedUser, UpdateSourceTrigger=PropertyChanged}"/>

Child Object:

public string UpdatedUser
{
    get
    {
        var parent = this.CrawlParentFindType<ParentObject>();

        if (Column4IsDirtyByValue || Column5IsDirtyByValue)
            return UpdatedByUserName;

        else return parent.UpdatedByUserName;
    }
}

public bool Column4IsDirtyByValue { get { return FieldManager.IsFieldDirty(Column4Property); } }
public bool Column5IsDirtyByValue{ get { return FieldManager.IsFieldDirty(Column5Property); } }


public string Column4
{
    get { return GetProperty(Column4Property); }
    set { SetProperty(Column4Property, value);  OnPropertyChanged(x => x.UpdatedUser); }
}

public string Column5
{
    get { return GetProperty(Column5Property); }
    set { SetProperty(Column5Property, value); OnPropertyChanged(x => x.UpdatedUser); }
}
Mihika
  • 1
  • 3

1 Answers1

0

Why is your code so complex? I think the reason is in OnPropertyChanged(x => x.UpdatedUser); You call UpdatedUser property changed, but the property doesn't have any setter. Code below should work for you, imho:

private bool _updatedUser;
public bool UpdatedUser
{
    get { return _updatedUser; }
    set { this.SetProperty(ref this._updatedUser, value); }
}

private string _column5;
public string Column5
{
    get { return _column5; }
    set
    {
        this.SetProperty(ref this._column5, value);

        UpdatedUserCheck();
    }
}

private void UpdatedUserCheck()
{
    this.UpdatedUser = // DO YOUR CHECK HERE
}
  • Thanks for the response! However, this didn't work. Now it updates the user name when I update the columns of child object (column5 and 4) but it doesn't update the user name when I update columns of parent object (1-3). And on initial load, the text box doesn't even show any user name. – Mihika Aug 23 '17 at 17:28
  • Check this: [link](https://stackoverflow.com/posts/38120468/revisions) . It involves an Action. Action is declared and invoked in the Child object. Action is defined in the Parent object. That means - upon MyAction.Invoke() in the Child, it can access/modify Parent's properties – Alexey Titov Aug 23 '17 at 17:52