Skip to content

Instantly share code, notes, and snippets.

@grokys
Created May 24, 2022 10:58
Show Gist options
  • Save grokys/35ed4379a430289e104849cdc78b8feb to your computer and use it in GitHub Desktop.
Save grokys/35ed4379a430289e104849cdc78b8feb to your computer and use it in GitHub Desktop.
diff --git a/samples/Sandbox/MainWindow.axaml b/samples/Sandbox/MainWindow.axaml
index 6929f192c75098fa26851573ddd5a3fb70d19e30..d6ddf70b0337675596b283de5e0c680e55c7002c 100644
--- a/samples/Sandbox/MainWindow.axaml
+++ b/samples/Sandbox/MainWindow.axaml
@@ -1,4 +1,20 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class="Sandbox.MainWindow">
+
+ <Grid RowDefinitions="Auto, *, Auto">
+ <TextBlock Text="Text" Name="TB" Grid.Row="0" Margin="0,0,0,4" />
+
+ <ListBox x:Name="LB" Grid.Row="1">
+ <ListBox.ItemTemplate>
+ <DataTemplate>
+ <TextBlock Text="{Binding #TB.Text}" />
+ </DataTemplate>
+ </ListBox.ItemTemplate>
+ </ListBox>
+
+ <Button Grid.Row="2" Content="Regenerate" Click="Button_OnClick" />
+
+ </Grid>
+
</Window>
diff --git a/samples/Sandbox/MainWindow.axaml.cs b/samples/Sandbox/MainWindow.axaml.cs
index 3d54036d29d6e108931327e266565540d0d52728..3ba097e02c8399b367791075c537241534f36d1c 100644
--- a/samples/Sandbox/MainWindow.axaml.cs
+++ b/samples/Sandbox/MainWindow.axaml.cs
@@ -1,21 +1,46 @@
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
using Avalonia;
using Avalonia.Controls;
+using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
-using Avalonia.Win32.WinRT.Composition;
namespace Sandbox
{
public class MainWindow : Window
{
+ private readonly ObservableCollection<string> _data;
+
public MainWindow()
{
this.InitializeComponent();
this.AttachDevTools();
+
+ var lb = this.GetControl<ListBox>("LB");
+
+ _data = new ObservableCollection<string>();
+
+ for (int i = 0; i < 50; i++)
+ {
+ _data.Add(i.ToString());
+ }
+
+ lb.Items = _data;
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
+
+ private void Button_OnClick(object sender, RoutedEventArgs e)
+ {
+ _data.Clear();
+
+ for (int i = 0; i < 50; i++)
+ {
+ _data.Add(i.ToString());
+ }
+ }
}
}
diff --git a/tests/Avalonia.Markup.UnitTests/Data/BindingTests_ElementName.cs b/tests/Avalonia.Markup.UnitTests/Data/BindingTests_ElementName.cs
index 69dfc6c0622fe559454d693075abd99455959514..86f80266827f6d53312b399b2be5f36b6c3adb4a 100644
--- a/tests/Avalonia.Markup.UnitTests/Data/BindingTests_ElementName.cs
+++ b/tests/Avalonia.Markup.UnitTests/Data/BindingTests_ElementName.cs
@@ -1,5 +1,6 @@
using System;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Data;
using Avalonia.Markup.Data;
using Avalonia.UnitTests;
@@ -162,5 +163,42 @@ public void Should_Bind_To_Later_Added_Element()
Assert.Same(source, target.Content);
}
+
+ [Fact]
+ public void Should_Actually_Work()
+ {
+ TextBlock source;
+
+ var root = new TestRoot
+ {
+ Child = new StackPanel
+ {
+ Children =
+ {
+ (source = new TextBlock
+ {
+ Name = "source",
+ Text = "foo",
+ })
+ }
+ }
+ };
+
+ root.RegisterChildrenNames();
+
+ var binding = new Binding
+ {
+ ElementName = "source",
+ Path = nameof(TextBlock.Text),
+ NameScope = new WeakReference<INameScope>(NameScope.GetNameScope(root))
+ };
+
+ for (int i = 0; i < 1000; i++)
+ {
+ var target = new TextBlock();
+
+ target.Bind(TextBlock.TextProperty, binding);
+ }
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment