You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
docs: add ICommand DependencyProperty example to control libraries guide - #24631
Adds a "Adding a bindable Command property" section to the control libraries guide, with an ICommand dependency property, a template Button binding, and XAML usage. The button handles CanExecuteChanged and enabled-state updates.
The control libraries guide showed how to create a templated control and set text, but had no example of exposing a bindable ICommand property - a common need when a control template contains a button that should invoke a view-model command. This adds a section showing how to register an ICommand-typed dependency property and bind it to the inner template button. The button handles command changes and CanExecute without overwriting an explicit IsEnabled value on the outer control. The guide notes that a control needing its own command-derived state must manage its handler across command changes and load/unload separately.
SerialDisposable and Disposable are both declared internal in the Uno source (SerialDisposable.cs:26 and Disposable.cs:24), so a control library consuming Uno.WinUI cannot access either type. The sample therefore still fails to compile even with this using; use a public disposal helper or track the subscribed ICommand and unsubscribe directly.
Dispose command subscription when control leaves visual tree
This subscription is never disposed when the control leaves the visual tree, so a long-lived command can retain the handler target and keep an abandoned control alive; it also continues receiving updates while unloaded. The framework's corresponding controls attach in EnterImpl and dispose in LeaveImpl (for example src/Uno.UI/UI/Xaml/Controls/MenuFlyout/MenuFlyoutItem/MenuFlyoutItem.mux.cs:382-422). Keep the command-change disposal, but scope the subscription to the control's active lifetime as well.
Replace the inaccessible internal disposable example with a public ICommand dependency property bound to the template Button. The Button handles CanExecuteChanged, enabled-state coercion and unloading without overriding a consumer's IsEnabled value.
If your control itself needs to react to `CanExecuteChanged` for a purpose beyond the template button, attach to the current command when the control loads, detach when it unloads or the command changes, and refresh the state when it loads again. Do not assign `IsEnabled = Command.CanExecute(...)` on the outer control: that would overwrite an `IsEnabled` value set by the consumer.
Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.
The reason will be displayed to describe this comment to others. Learn more.
Thanks @adarsh6980, this is a nice addition. I checked the sample against WinUI conventions and the Uno ButtonBase source: DependencyProperty.Register with typeof(ICommand), the owner type and PropertyMetadata(null) are correct, and {TemplateBinding Command} works as written. ButtonBase does subscribe to CanExecuteChanged on enter and unsubscribe on leave (weakly), and it coerces IsEnabled without overwriting an explicit false, so the claims in the text hold.
Optional nits, not blocking:
{x:Bind ViewModel.SaveCommand} defaults to OneTime. That's fine for a command that never changes, but you could mention Mode=OneWay for view models that swap the command later.
Consider adding a line noting that this snippet replaces the Grid/TextBlock template from the earlier step.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a "Adding a bindable Command property" section to the control libraries guide, with an
ICommanddependency property, a templateButtonbinding, and XAML usage. The button handlesCanExecuteChangedand enabled-state updates.GitHub Issue: closes #19156
PR Type:
📚 Documentation content changes
What changed? 🚀
The control libraries guide showed how to create a templated control and set text, but had no example of exposing a bindable
ICommandproperty - a common need when a control template contains a button that should invoke a view-model command. This adds a section showing how to register anICommand-typed dependency property and bind it to the inner template button. The button handles command changes andCanExecutewithout overwriting an explicitIsEnabledvalue on the outer control. The guide notes that a control needing its own command-derived state must manage its handler across command changes and load/unload separately.PR Checklist ✅
Screenshots Compare Test Runresults.