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

WPF0041 is raised when setting the properties of private elements #356

Open
Artfunkel opened this issue Feb 5, 2022 · 4 comments
Open

Comments

@Artfunkel
Copy link

The intent of this warning is to prevent data bindings from being unintentionally replaced with a local value, and that is a very helpful idea.

Unfortunately, the warning is raised in far too many bogus circumstances to actually be helpful. A UserControl/Window has complete control of the elements privately defined within its XAML file, and setting local values on them is a completely normal procedure. The author knows that the property is not bound, because if they didn't bind it then nobody did.

The warning can be bogus even in plain FrameworkElement types, since they can create and control their own private children.

When I added WpfAnalyzers to my company's large WPF codebase, it raised a whopping 533 WPF0041 warnings. 99-100% of these (it's hard to be sure, of course...) fall into one of the above categories. I would have to disable this warning entirely before committing anything to master.

My proposals are these:

  • Raise WPF0041 when a FrameworkElement class is setting the value of a public, writeable DependencyProperty declared or inherited by that specific class itself.
  • Raise WPF0041 when an attached property's PropertyChangedCallback is setting the value of any public, writeable DependencyProperty.
  • Do not raise WPF0041 when a UserControl or Window sets any of its own properties.
@JohanLarsson
Copy link
Collaborator

JohanLarsson commented Feb 5, 2022

We can tweak the rules and maybe we should make the default severity Info so user can set desired severity.
In applications this rule should probably be disabled most of the times.
In library code it can be helpful.

@Artfunkel
Copy link
Author

Artfunkel commented Feb 5, 2022

Configuring severity isn't an effective solution for us, alas. We have a library assembly where these warnings are welcome, but also many FrameworkElement classes in our application code, nestled among UserControls for which those same warnings need to be disabled.

We would have to disable the warning for that project then re-enable it per file, which is prone to error, and then beyond that configure it per line in some cases. It would be much easier to turn it off entirely and then write a smarter version of our own that did the right thing by itself.

@JohanLarsson
Copy link
Collaborator

You know you can configure per project using .editorconfig or .ruleset right?

@Artfunkel
Copy link
Author

Yes, that's the exact operation that I'm talking about. I edited my previous comment for clarity.

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