Skip to content

Commit

Permalink
Ensure that Grid is propagating the BindingContext to the row/col def…
Browse files Browse the repository at this point in the history
…initions (#7301)

Fixes #4609
  • Loading branch information
hartez committed May 23, 2022
1 parent 347751a commit d5981b9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
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));
}
}
}

0 comments on commit d5981b9

Please sign in to comment.