Skip to content

docs: add ICommand DependencyProperty example to control libraries guide - #24631

Open
adarsh6980 wants to merge 3 commits into
unoplatform:masterfrom
adarsh6980:adarsh6980-patch-1
Open

adarsh6980 wants to merge 3 commits into
unoplatform:masterfrom
adarsh6980:adarsh6980-patch-1

Conversation

@adarsh6980

@adarsh6980 adarsh6980 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

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 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.

PR Checklist ✅

Added instructions for creating a bindable Command property in custom controls, including implementation details and XAML usage.
Copilot AI lite review requested due to automatic review settings September 24, 2026 11:27
@CLAassistant

CLAassistant commented Sep 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Code samples are missing the Microsoft.UI.Xaml.Controls namespace import.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

Adds documentation for a bindable ICommand dependency property in templated control libraries.

Changes:

  • Adds command registration and CanExecuteChanged handling.
  • Demonstrates XAML command binding.
File Description
doc/​articles/​guides/​how-to-create-control-libraries.md Adds bindable command-property guidance and examples.

@unodevops

Copy link
Copy Markdown
Contributor

Copilot AI review requested due to automatic review settings September 26, 2026 21:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The example has a critical enabled-state issue and additional compilation, lifecycle, and binding problems.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (1)
Previously missed (2)

In code that hasn't changed since last review

Medium severity Use publicly accessible disposal API for command subscription

doc/​articles/​guides/​how-to-create-control-libraries.md:67

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.

Medium severity Dispose command subscription when control leaves visual tree

doc/​articles/​guides/​how-to-create-control-libraries.md:100

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.

@unodevops

Copy link
Copy Markdown
Contributor

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.
Copilot AI review requested due to automatic review settings September 28, 2026 08:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Clarify binding behavior and include the documented SerialDisposable implementation pattern.

Review effort: Lite
Findings: 1 Low severity

Open (1)
Resolved since last review (1)

<myControlLib:MyTemplatedControl Command="{x:Bind ViewModel.SaveCommand}" />
```

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.
@unodevops

Copy link
Copy Markdown
Contributor

@mergify

mergify Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • Queue this pull request

@MartinZikmund MartinZikmund left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Please add Command implementation sample for creating own Control Librarys with Templated Control Template-File

5 participants