csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
4.5 KiB
166 lines
4.5 KiB
using System;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Data;
|
|
using Avalonia.Markup.Data;
|
|
using Avalonia.UnitTests;
|
|
using Xunit;
|
|
|
|
namespace Avalonia.Markup.UnitTests.Data
|
|
{
|
|
public class BindingTests_ElementName : ScopedTestBase
|
|
{
|
|
[Fact]
|
|
public void Should_Bind_To_Element_Path()
|
|
{
|
|
TextBlock target;
|
|
var root = new TestRoot
|
|
{
|
|
Child = new StackPanel
|
|
{
|
|
Children =
|
|
{
|
|
new TextBlock
|
|
{
|
|
Name = "source",
|
|
Text = "foo",
|
|
},
|
|
(target = new TextBlock
|
|
{
|
|
Name = "target",
|
|
})
|
|
}
|
|
}
|
|
};
|
|
|
|
root.RegisterChildrenNames();
|
|
|
|
var binding = new Binding
|
|
{
|
|
ElementName = "source",
|
|
Path = "Text",
|
|
NameScope = new WeakReference<INameScope>(NameScope.GetNameScope(root))
|
|
};
|
|
|
|
target.Bind(TextBox.TextProperty, binding);
|
|
|
|
Assert.Equal("foo", target.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Bind_To_Element()
|
|
{
|
|
TextBlock source;
|
|
ContentControl target;
|
|
|
|
var root = new TestRoot
|
|
{
|
|
Child = new StackPanel
|
|
{
|
|
Children =
|
|
{
|
|
(source = new TextBlock
|
|
{
|
|
Name = "source",
|
|
Text = "foo",
|
|
}),
|
|
(target = new ContentControl
|
|
{
|
|
Name = "target",
|
|
})
|
|
}
|
|
}
|
|
};
|
|
root.RegisterChildrenNames();
|
|
|
|
var binding = new Binding
|
|
{
|
|
ElementName = "source",
|
|
NameScope = new WeakReference<INameScope>(NameScope.GetNameScope(root))
|
|
};
|
|
|
|
target.Bind(ContentControl.ContentProperty, binding);
|
|
|
|
Assert.Same(source, target.Content);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Bind_To_Later_Added_Element_Path()
|
|
{
|
|
TextBlock target;
|
|
StackPanel stackPanel;
|
|
|
|
var root = new TestRoot
|
|
{
|
|
Child = stackPanel = new StackPanel
|
|
{
|
|
Children =
|
|
{
|
|
(target = new TextBlock
|
|
{
|
|
Name = "target",
|
|
}),
|
|
}
|
|
}
|
|
};
|
|
root.RegisterChildrenNames();
|
|
|
|
var binding = new Binding
|
|
{
|
|
ElementName = "source",
|
|
Path = "Text",
|
|
NameScope = new WeakReference<INameScope>(NameScope.GetNameScope(root))
|
|
};
|
|
|
|
target.Bind(TextBox.TextProperty, binding);
|
|
|
|
stackPanel.Children.Add(new TextBlock
|
|
{
|
|
Name = "source",
|
|
Text = "foo",
|
|
});
|
|
root.RegisterChildrenNames();
|
|
Assert.Equal("foo", target.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Bind_To_Later_Added_Element()
|
|
{
|
|
ContentControl target;
|
|
StackPanel stackPanel;
|
|
|
|
var root = new TestRoot
|
|
{
|
|
Child = stackPanel = new StackPanel
|
|
{
|
|
Children =
|
|
{
|
|
(target = new ContentControl
|
|
{
|
|
Name = "target",
|
|
}),
|
|
}
|
|
}
|
|
};
|
|
root.RegisterChildrenNames();
|
|
|
|
var binding = new Binding
|
|
{
|
|
ElementName = "source",
|
|
NameScope = new WeakReference<INameScope>(NameScope.GetNameScope(root))
|
|
};
|
|
|
|
target.Bind(ContentControl.ContentProperty, binding);
|
|
|
|
var source = new TextBlock
|
|
{
|
|
Name = "source",
|
|
Text = "foo",
|
|
};
|
|
|
|
stackPanel.Children.Add(source);
|
|
root.RegisterChildrenNames();
|
|
|
|
Assert.Same(source, target.Content);
|
|
}
|
|
}
|
|
}
|
|
|