Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnableDataBinding PropertyChanged PropertyChangedEventArgs.PropertyName always null #522

Closed
djwork opened this issue Jun 27, 2024 · 2 comments

Comments

@djwork
Copy link

djwork commented Jun 27, 2024

Hi mganss
I am trying to use the PropertyChanged event handler but I seem to be getting null from PropertyChangedEventArgs.PropertyName.

I noticed that "propertyName = null" is being passed to the PropertyChangedEventArgs constructor inside the generated OnPropertyChanged method.

protected virtual void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberNameAttribute()] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName = null));
}

I manually changed "PropertyChangedEventArgs(propertyName = null))" -> "PropertyChangedEventArgs(propertyName))" and the PropertyChangedEventArgs.PropertyName came through correctly.

I think this is a bug in ClassModel.Generate()

var param = new CodeParameterDeclarationExpression(typeof(string), "propertyName = null");
param.CustomAttributes.Add(new(TypeRef<System.Runtime.CompilerServices.CallerMemberNameAttribute>()));
var threadSafeDelegateInvokeExpression = new CodeSnippetExpression($"{propertyChangedEvent.Name}?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs({param.Name}))");

param's Name property is set to "propertyName = null" via the constructor, which is correct because CallerMemberNameAttribute requires the parameter to have a default value.
But the string passed to the CodeSnippetExpression constructor reuses the param.Name which is what I believe is creating the bug, because the Name property contains the parameter default expression:
image

So what do think about changing:

var threadSafeDelegateInvokeExpression = new CodeSnippetExpression($"{propertyChangedEvent.Name}?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs({param.Name}))");

to:

var threadSafeDelegateInvokeExpression = new CodeSnippetExpression($"{propertyChangedEvent.Name}?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName))");
@mganss mganss closed this as completed in 8d8f5ff Jun 28, 2024
@mganss
Copy link
Owner

mganss commented Jun 28, 2024

Thanks for catching this one and also providing the fix!

@mganss
Copy link
Owner

mganss commented Jun 28, 2024

Released in 2.1.1155.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants