-
Notifications
You must be signed in to change notification settings - Fork 1.9k
ContentPresenter: Propagate binding context to children with explicit TemplateBinding #30880
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
Merged
kubaflo
merged 3 commits into
dotnet:inflight/current
from
HarishwaranVijayakumar:fix-23797
Feb 23, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
115 changes: 115 additions & 0 deletions
115
src/Controls/tests/TestCases.HostApp/Issues/Issue23797.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using System.ComponentModel; | ||
| using System.Windows.Input; | ||
| using Microsoft.Maui.Controls; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 23797, "Binding context in ContentPresenter", PlatformAffected.All)] | ||
| public class Issue23797 : ContentPage | ||
| { | ||
| public static Issue23797_ViewModel Issue23797_ViewModel { get; } = new Issue23797_ViewModel(); | ||
|
|
||
| public Issue23797() | ||
| { | ||
| Title = "Issue23797"; | ||
|
|
||
| var stackLayout = new StackLayout | ||
| { | ||
| Spacing = 20, | ||
| Padding = 20 | ||
| }; | ||
|
|
||
| var label = new Label | ||
| { | ||
| FontSize = 16, | ||
| BackgroundColor = Colors.LightGray, | ||
| Padding = 10, | ||
| AutomationId = "CurrentMessageLabel" | ||
| }; | ||
| label.SetBinding(Label.TextProperty, new Binding("Message", source: Issue23797_ViewModel)); | ||
| stackLayout.Children.Add(label); | ||
|
|
||
| var customControl = new CustomControlWithCustomContent_Issue23797 | ||
| { | ||
| BindingContext = Issue23797_ViewModel | ||
| }; | ||
|
|
||
| var button = new Button | ||
| { | ||
| Text = "Click to change to 'success'", | ||
| BackgroundColor = Colors.LightBlue, | ||
| AutomationId = "Issue23797Btn" | ||
| }; | ||
| button.SetBinding(Button.CommandProperty, new Binding("TestCommand")); | ||
|
|
||
| customControl.MyContent = button; | ||
| stackLayout.Children.Add(customControl); | ||
|
|
||
| Content = stackLayout; | ||
| } | ||
| } | ||
|
|
||
| public class CustomControlWithCustomContent_Issue23797 : ContentView | ||
| { | ||
| public static readonly BindableProperty MyContentProperty = BindableProperty.Create( | ||
| nameof(MyContent), | ||
| typeof(View), | ||
| typeof(CustomControlWithCustomContent_Issue23797), | ||
| null); | ||
|
|
||
| public View MyContent | ||
| { | ||
| get => (View)GetValue(MyContentProperty); | ||
| set => SetValue(MyContentProperty, value); | ||
| } | ||
|
|
||
| public CustomControlWithCustomContent_Issue23797() | ||
| { | ||
| ControlTemplate = new ControlTemplate(() => | ||
| { | ||
| var border = new Border | ||
| { | ||
| Stroke = Colors.Red, | ||
| StrokeThickness = 2, | ||
| Padding = 10 | ||
| }; | ||
|
|
||
| var contentPresenter = new ContentPresenter(); | ||
| contentPresenter.SetBinding(ContentPresenter.ContentProperty, new Binding(nameof(MyContent), source: RelativeBindingSource.TemplatedParent)); | ||
|
|
||
| border.Content = contentPresenter; | ||
| return border; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public class Issue23797_ViewModel : INotifyPropertyChanged | ||
| { | ||
| private string _message = "failure"; | ||
|
|
||
| public string Message | ||
| { | ||
| get => _message; | ||
| set | ||
| { | ||
| _message = value; | ||
| OnPropertyChanged(); | ||
| } | ||
| } | ||
|
|
||
| public ICommand TestCommand { get; } | ||
|
|
||
| public Issue23797_ViewModel() | ||
| { | ||
| TestCommand = new Command(() => | ||
| { | ||
| Message = "success"; | ||
| }); | ||
| } | ||
|
|
||
| public event PropertyChangedEventHandler PropertyChanged; | ||
| protected virtual void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) | ||
| { | ||
| PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue23797.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue23797 : _IssuesUITest | ||
| { | ||
| public Issue23797(TestDevice testDevice) : base(testDevice) | ||
| { | ||
| } | ||
|
|
||
| public override string Issue => "Binding context in ContentPresenter"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.Layout)] | ||
| public void ContentPresenterShouldPropagateBindingContextForTemplateBindings() | ||
| { | ||
| App.WaitForElement("Issue23797Btn"); | ||
| App.Tap("Issue23797Btn"); | ||
| var messageAfterClick = App.WaitForElement("CurrentMessageLabel").GetText(); | ||
| Assert.That(messageAfterClick, Is.EqualTo("success")); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we know why this comment is the exact opposite of this PR?