Browse Source

Merge pull request #1246 from AvaloniaUI/fixes/1245-popup-selecteditem-binding

Fix data context change notification
pull/1254/head
danwalmsley 8 years ago
committed by GitHub
parent
commit
c7387d44ac
  1. 4
      src/Avalonia.Controls/Control.cs
  2. 44
      tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs

4
src/Avalonia.Controls/Control.cs

@ -774,7 +774,9 @@ namespace Avalonia.Controls
foreach (var child in control.LogicalChildren)
{
if (child is Control c && !c.IsSet(DataContextProperty))
if (child is Control c &&
c.InheritanceParent == control &&
!c.IsSet(DataContextProperty))
{
DataContextNotifying(c, notifying);
}

44
tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs

@ -249,6 +249,37 @@ namespace Avalonia.Controls.UnitTests.Primitives
}
}
[Fact]
public void DataContextBeginUpdate_Should_Not_Be_Called_For_Controls_That_Dont_Inherit()
{
using (CreateServices())
{
TestControl child;
var popup = new Popup
{
Child = child = new TestControl(),
DataContext = "foo",
};
var beginCalled = false;
child.DataContextBeginUpdate += (s, e) => beginCalled = true;
// Test for #1245. Here, the child's logical parent is the popup but it's not yet
// attached to a visual tree because the popup hasn't been opened.
Assert.Same(popup, ((ILogical)child).LogicalParent);
Assert.Same(popup, child.InheritanceParent);
Assert.Null(child.GetVisualRoot());
popup.Open();
// #1245 was caused by the fact that DataContextBeginUpdate was called on `target`
// when the PopupRoot was created, even though PopupRoot isn't the
// InheritanceParent of child.
Assert.False(beginCalled);
}
}
private static IDisposable CreateServices()
{
var result = AvaloniaLocator.EnterScope();
@ -304,5 +335,18 @@ namespace Avalonia.Controls.UnitTests.Primitives
private class PopupContentControl : ContentControl
{
}
private class TestControl : Decorator
{
public event EventHandler DataContextBeginUpdate;
public new IAvaloniaObject InheritanceParent => base.InheritanceParent;
protected override void OnDataContextBeginUpdate()
{
DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
base.OnDataContextBeginUpdate();
}
}
}
}

Loading…
Cancel
Save