Browse Source

use Delayed Bindings for all properties except datacontext

pull/916/head
Andrey Kunchev 10 years ago
parent
commit
a010d5c37a
  1. 10
      src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlType.cs
  2. 27
      src/Markup/Avalonia.Markup.Xaml/PortableXaml/XamlBinding.cs
  3. 19
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests.cs

10
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlType.cs

@ -324,15 +324,15 @@ namespace Avalonia.Markup.Xaml.PortableXaml
}
}
public void ApplyBinding(IAvaloniaObject obj, IBinding binding)
private void ApplyBinding(IAvaloniaObject obj, IBinding binding)
{
var control = obj as IControl;
var property = Property;
var xamlBinding = binding as XamlBinding;
//if (control != null && property != Control.DataContextProperty)
// DelayedBinding.Add(control, property, binding);
//else
if (xamlBinding != null)
if (control != null && property != Control.DataContextProperty)
DelayedBinding.Add(control, property, binding);
else if (xamlBinding != null)
obj.Bind(property, xamlBinding.Value, xamlBinding.Anchor?.Target);
else
obj.Bind(property, binding);

27
src/Markup/Avalonia.Markup.Xaml/PortableXaml/XamlBinding.cs

@ -5,8 +5,6 @@ using Portable.Xaml;
using Portable.Xaml.ComponentModel;
using Portable.Xaml.Markup;
using System;
using System.Linq;
namespace Avalonia.Markup.Xaml.PortableXaml
{
@ -32,25 +30,16 @@ namespace Avalonia.Markup.Xaml.PortableXaml
{
object anchor = null;
//// The target is not a control, so we need to find an anchor that will let us look
//// up named controls and style resources. First look for the closest IControl in
//// the TopDownValueContext.
//anchor = context.TopDownValueContext.StoredInstances
// .Select(x => x.Instance)
// .OfType<IControl>()
// .LastOrDefault();
// The target is not a control, so we need to find an anchor that will let us look
// up named controls and style resources. First look for the closest IControl in
// the context.
anchor = context.GetFirstAmbientValue<IControl>();
//// If a control was not found, then try to find the highest-level style as the XAML
//// file could be a XAML file containing only styles.
// anchor = context.TopDownValueContext.StoredInstances
// .Select(x => x.Instance)
// .OfType<IStyle>()
// .FirstOrDefault();
var rs = context.GetService<IRootObjectProvider>().RootObject as IStyle;
return anchor ?? rs ?? context.GetLastOrDefaultAmbientValue<IStyle>();
// If a control was not found, then try to find the highest-level style as the XAML
// file could be a XAML file containing only styles.
return anchor ??
context.GetService<IRootObjectProvider>()?.RootObject as IStyle ??
context.GetLastOrDefaultAmbientValue<IStyle>();
}
private XamlBinding(IBinding binding, object anchor)

19
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests.cs

@ -147,26 +147,27 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
}
#if OMNIXAML
[Fact(Skip = "OmniXaml doesn't support nested markup extensions. #119")]
#else
[Fact]
#endif
public void Binding_To_Self_Works()
{
var xaml = @"
<ContentControl xmlns='https://github.com/avaloniaui'
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<TextBlock Name='textblock' Text='{Binding Tag, RelativeSource={RelativeSource Self}}'/>
</ContentControl>";
</Window>";
var window = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
var textBlock = (TextBlock)window.Content;
var window = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
var textBlock = (TextBlock)window.Content;
textBlock.Tag = "foo";
textBlock.Tag = "foo";
Assert.Equal("foo", textBlock.Text);
Assert.Equal("foo", textBlock.Text);
}
}
[Fact]

Loading…
Cancel
Save