Skip to content

Commit

Permalink
Merge pull request #2655 from cwensley/curtis/fix-binding-context-wit…
Browse files Browse the repository at this point in the history
…h-parent-change

Trigger DataContextChanged when parent change causes DataContext to be null
  • Loading branch information
cwensley authored May 27, 2024
2 parents e5e7d3e + b52fb5b commit 3dc13f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Eto/Forms/Binding/BindableWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ public Widget Parent
get => Properties.Get<Widget>(Parent_Key);
internal protected set
{
var dataContext = DataContext;
if (Properties.TrySet(Parent_Key, value))
{
if (!HasDataContext && !(DataContext is null))
if (!HasDataContext && !ReferenceEquals(DataContext, dataContext))
{
IsDataContextChanging = true;
OnDataContextChanged(EventArgs.Empty);
Expand Down
24 changes: 24 additions & 0 deletions test/Eto.Test/UnitTests/Forms/DataContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,30 @@ public void ChangingDataContextShouldNotSetValues() => Shown(
Assert.AreEqual(0, model1.SelectedIndex, "#3 - Model 1 was changed");
Assert.AreEqual(1, model2.SelectedIndex, "#4 - Model 2 was changed");
});

[Test]
public void RemovingFromParentShouldTriggerBindingChanged() => Invoke(() =>
{
int parentDataContextChanged = 0;
int childDataContextChanged = 0;
var panel = new Panel();
panel.DataContextChanged += (sender, e) => parentDataContextChanged++;
panel.DataContext = new MyViewModelWithEquals { ID = 10 };
Assert.AreEqual(1, parentDataContextChanged);
var child = new Panel();
child.DataContextChanged += (sender, e) => childDataContextChanged++;
panel.Content = child;
Assert.AreEqual(1, childDataContextChanged);
Assert.AreSame(child.DataContext, panel.DataContext);
panel.Content = null;
Assert.AreEqual(2, childDataContextChanged);
Assert.AreEqual(1, parentDataContextChanged);
Assert.IsNull(child.DataContext);
});
}
}

0 comments on commit 3dc13f7

Please sign in to comment.