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

Ensure that Grid is propagating the BindingContext to the row/col definitions #7301

Merged
merged 1 commit into from May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Controls/src/Core/Layout/Grid.cs
Expand Up @@ -369,6 +369,8 @@ void DefinitionsChanged(object sender, EventArgs args)
_rowDefs = null;
_colDefs = null;

UpdateRowColumnBindingContexts();

InvalidateMeasure();
}

Expand All @@ -378,6 +380,29 @@ protected override void InvalidateMeasure()
(this as IView)?.InvalidateMeasure();
}

protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
UpdateRowColumnBindingContexts();
}

void UpdateRowColumnBindingContexts()
{
var bindingContext = BindingContext;

RowDefinitionCollection rowDefs = RowDefinitions;
for (var i = 0; i < rowDefs.Count; i++)
{
SetInheritedBindingContext(rowDefs[i], bindingContext);
}

ColumnDefinitionCollection colDefs = ColumnDefinitions;
for (var i = 0; i < colDefs.Count; i++)
{
SetInheritedBindingContext(colDefs[i], bindingContext);
}
}

class GridInfo
{
public int Row { get; set; }
Expand Down
56 changes: 56 additions & 0 deletions src/Controls/tests/Core.UnitTests/Layouts/GridLayoutTests.cs
Expand Up @@ -128,5 +128,61 @@ static void AssertInvalidated(IViewHandler handler)
{
handler.Received().Invoke(Arg.Is(nameof(IView.InvalidateMeasure)), Arg.Any<object>());
}

[Test]
public void RowDefinitionsGetBindingContext()
{
var def = new RowDefinition();
var def2 = new RowDefinition();

var grid = new Grid()
{
RowDefinitions = new RowDefinitionCollection
{
def
}
};

var context = new object();

Assert.That(def.BindingContext, Is.Null);
Assert.That(def2.BindingContext, Is.Null);

grid.BindingContext = context;

Assert.That(def.BindingContext, Is.EqualTo(context));

grid.RowDefinitions.Add(def2);

Assert.That(def2.BindingContext, Is.EqualTo(context));
}

[Test]
public void ColumnDefinitionsGetBindingContext()
{
var def = new ColumnDefinition();
var def2 = new ColumnDefinition();

var grid = new Grid()
{
ColumnDefinitions = new ColumnDefinitionCollection
{
def
}
};

var context = new object();

Assert.That(def.BindingContext, Is.Null);
Assert.That(def2.BindingContext, Is.Null);

grid.BindingContext = context;

Assert.That(def.BindingContext, Is.EqualTo(context));

grid.ColumnDefinitions.Add(def2);

Assert.That(def2.BindingContext, Is.EqualTo(context));
}
}
}