Introduce Command Binding, Bindable ToolStripItems and DataContext to WinForms#7143
Conversation
* Add CommandBinding to ToolStripItem.
* Also, refactor things a bit based on the changes.
* DataContext Property * OnParentDataContextChanged Method * OnDataContextChanged Method * DataContextChanged Event
After having an internal team discussion, we decided we should be in naming-parity with XAML based languages, and not so much afraid about breaking things due to already existing Command properties on Button-derived classes, since they are separated by namespaces anyway.
RussKie
left a comment
There was a problem hiding this comment.
As discussed, please bring the API proposal up-to-date. Few nits and suggestions.
src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/IBindableCommandProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/IBindableCommandProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ExecuteCommandEventArgs.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
* Add DataContext CodeDom Serialization controlling
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ICommandProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ICommandProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs
Outdated
Show resolved
Hide resolved
|
@gewarren when you have a chance, may I get you to review from the docs perspective? |
src/System.Windows.Forms/src/System/Windows/Forms/ICommandProvider.cs
Outdated
Show resolved
Hide resolved
gewarren
left a comment
There was a problem hiding this comment.
In case it's helpful, the guidelines for documenting event-related APIs are here: https://github.com/dotnet/dotnet-api-docs/wiki/Summary%3A-Event-related
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/BindableComponent.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/BindableComponent.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/BindableComponent.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ICommandBindingTargetProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ICommandBindingTargetProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ICommandBindingTargetProvider.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/ToolStripComboBox.cs
Outdated
Show resolved
Hide resolved
…r ToolStripButton.
* Fixing an ambient behavior bug in DataContext.
|
@KlausLoeffelmann I just come across this blog https://devblogs.microsoft.com/dotnet/announcing-the-dotnet-community-toolkit-800/. |
|
I don't see why not! |
src/System.Windows.Forms/src/System/Windows/Forms/BindableComponent.cs
Outdated
Show resolved
Hide resolved
90ed7df
| <value>Expanded</value> | ||
| </data> | ||
| <data name="ToolStripItemOnCommandChangedDescr" xml:space="preserve"> | ||
| <value>Occurs whenever the bindable command changes.</value> |
There was a problem hiding this comment.
This wording is common for event handlers, and it's used in other parts of the codebase.
| namespace System.Windows.Forms | ||
| { | ||
| /// <summary> | ||
| /// Base class for components which provide properties which can be |
There was a problem hiding this comment.
Please use 2 whitespaces after the ///
| protected override void OnFontChanged(EventArgs e) | ||
| { | ||
| // Perf: don't call base, we don't care if the font changes | ||
| #pragma warning disable CA2252 // Suppress 'Opt in to preview features' (https://aka.ms/dotnet-warnings/preview-features) |
There was a problem hiding this comment.
I don't understand why we have the preview warning when raising one of the old events.
There was a problem hiding this comment.
Cut off the dependencies, when the consumer does not use the Preview feature directly. If we didn't do this, also using OnFontChanged would have been considered a Preview feature, and so on, and so on.
It's removed now - no longer in Preview.
| { | ||
| RaiseEvent(s_clickEvent, e); | ||
|
|
||
| // We won't let the preview feature warnings bubble further up beyond this point. |
There was a problem hiding this comment.
I don't really follow this comment. Do you mean that application that overrides this method will not show the warning?
There was a problem hiding this comment.
No, but at one point we needed to protect the WinForms runtime against the Preview warnings themselves. It's gone now.
| protected virtual void OnCommandParameterChanged(EventArgs e) => RaiseEvent(s_commandParameterChangedEvent, e); | ||
|
|
||
| /// <summary> | ||
| /// Called in the context of <see cref="OnClick(EventArgs)"/> to invoke <see cref="System.Windows.Input.ICommand.Execute(object?)"/> if the context allows. |
There was a problem hiding this comment.
nit; this line is too long
This fixes #4895.
ICommandBindingTargetProviderwith Default Interface Methods holding 90% of theCommandlogic, so it can be easily reimplemented in other Components/Controls where is makes sense. This is no longer public, but internal, just to not repeat implementation code.BindableComponentto have a Component which can be bound at Design time the same way it works withControlalready (both are implementingIBindableComponent).ICommandBindingTargetProviderintoToolStripItemto let it have aCommandproperty of typeICommandwhich can be bound to from Controllers/ViewModels.ICommandBindingTargetProviderintoButtonBaseto let it have aCommandproperty of typeICommandwhich can be bound to from Controllers/ViewModels.DataContextProperty as an ambient property along withDataContextChangedevent,OnDataContextChangedandOnParentDataContextChanged.CommandEventArgs, addedCommandParameterproperty).Update: After having an internal team discussion, we decided to rename
BindableCommandback to be justCommand.Microsoft Reviewers: Open in CodeFlow