Browse Source

Make AncestorType required for ancestor searching via the visual tree.

pull/1209/head
Jeremy Koritzinsky 8 years ago
parent
commit
f9b578ab0e
  1. 5
      src/Markup/Avalonia.Markup.Xaml/Data/Binding.cs
  2. 17
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests_RelativeSource.cs

5
src/Markup/Avalonia.Markup.Xaml/Data/Binding.cs

@ -126,6 +126,11 @@ namespace Avalonia.Markup.Xaml.Data
}
else if (RelativeSource.Mode == RelativeSourceMode.FindAncestor)
{
if (RelativeSource.Tree == TreeType.Visual && RelativeSource.AncestorType == null)
{
throw new InvalidOperationException("AncestorType must be set for RelativeSourceModel.FindAncestor when searching the visual tree.");
}
observer = CreateFindAncestorObserver(
(target as IControl) ?? (anchor as IControl),
RelativeSource,

17
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests_RelativeSource.cs

@ -3,6 +3,7 @@
using Avalonia.Controls;
using Avalonia.UnitTests;
using System;
using Xunit;
namespace Avalonia.Markup.Xaml.UnitTests.Xaml
@ -78,7 +79,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
}
[Fact]
public void Binding_To_First_Ancestor_Without_AncestorType_Uses_LogicalTree()
public void Binding_To_First_Ancestor_Without_AncestorType_Throws_Exception()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
@ -93,13 +94,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
</Border>
</Window>";
var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml);
var contentControl = window.FindControl<ContentControl>("contentControl");
var button = window.FindControl<Button>("button");
window.ApplyTemplate();
Assert.Equal("contentControl", button.Content);
Assert.Throws<InvalidOperationException>( () => loader.Load(xaml));
}
}
@ -149,11 +144,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
var button = window.FindControl<Button>("button");
window.ApplyTemplate();
// This isn't needed in `Binding_To_First_Ancestor_Without_AncestorType_Uses_LogicalTree` -
// why is it needed to get non-null value here?
//contentControl.ApplyTemplate();
Assert.Equal("contentControl", button.Content);
}
}

Loading…
Cancel
Save