Skip to content

Commit

Permalink
Trigger DataContextChanged when parent change causes DataContext to b…
Browse files Browse the repository at this point in the history
…e null
  • Loading branch information
cwensley committed May 27, 2024
1 parent dc8a039 commit b52fb5b
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 b52fb5b

Please sign in to comment.