the control already has a visual parent Error
See original GitHub issueI tried to add a lot of button to a ListBox But I keep getting this error : the control already has a visual parent
The error is trigered from here: public static void Main(string[] args) => BuildAvaloniaApp() .StartWithClassicDesktopLifetime(args);
the StackTrace :
at Avalonia.Visual.ValidateVisualChild(IVisual c) at Avalonia.Collections.AvaloniaList
1.Add(T item)
at Avalonia.Controls.Presenters.ContentPresenter.UpdateChild()
at Avalonia.Controls.Presenters.ContentPresenter.ContentChanged(AvaloniaPropertyChangedEventArgs e)
at Avalonia.Controls.Presenters.ContentPresenter.<>c.<.cctor>b__14_0(ContentPresenter x, AvaloniaPropertyChangedEventArgs e)
at System.Reactive.Subjects.Subject1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\Subject.cs:line 148 at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority) at Avalonia.AvaloniaObject.PriorityValueChanged(AvaloniaProperty property, Int32 priority, Object oldValue, Object newValue) at Avalonia.PriorityValue.SetAndNotify(ValueTuple
2& backing, ValueTuple2 update) at Avalonia.PriorityValue.Avalonia.Utilities.ISetAndNotifyHandler<(System.Object,System.Int32)>.HandleSetAndNotify(AvaloniaProperty property, ValueTuple
2& backing, ValueTuple2 value) at Avalonia.Utilities.DeferredSetter
1.SetAndNotifyCallback[TValue](AvaloniaProperty property, ISetAndNotifyHandler1 setAndNotifyHandler, TValue& backing, TValue value) at Avalonia.PriorityValue.UpdateValue(Object value, Int32 priority) at Avalonia.Reactive.SingleSubscriberObservableBase
1.PublishNext(T value)
at Avalonia.Data.TemplateBinding.PublishValue()
at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
at Avalonia.AvaloniaObject.PriorityValueChanged(AvaloniaProperty property, Int32 priority, Object oldValue, Object newValue)
at Avalonia.PriorityValue.SetAndNotify(ValueTuple2& backing, ValueTuple
2 update)
at Avalonia.PriorityValue.Avalonia.Utilities.ISetAndNotifyHandler<(System.Object,System.Int32)>.HandleSetAndNotify(AvaloniaProperty property, ValueTuple2& backing, ValueTuple
2 value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotifyCallback[TValue](AvaloniaProperty property, ISetAndNotifyHandler
1 setAndNotifyHandler, TValue& backing, TValue value)
at Avalonia.PriorityValue.UpdateValue(Object value, Int32 priority)
at Avalonia.ValueStore.AddValue(AvaloniaProperty property, Object value, Int32 priority)
at Avalonia.AvaloniaObject.SetStyledValue(AvaloniaProperty property, Object value, BindingPriority priority)
at Avalonia.AvaloniaObject.SetValue(AvaloniaProperty property, Object value, BindingPriority priority)
at Avalonia.Controls.Generators.ItemContainerGenerator1.TryRecycle(Int32 oldIndex, Int32 newIndex, Object item) at Avalonia.Controls.Presenters.ItemVirtualizerSimple.RecycleContainersForMove(Int32 delta) at Avalonia.Controls.Presenters.ItemVirtualizerSimple.set_OffsetValue(Double value) at Avalonia.Controls.Presenters.ItemVirtualizer.set_Offset(Vector value) at Avalonia.Controls.Presenters.ItemsPresenter.Avalonia.Controls.Primitives.IScrollable.set_Offset(Vector value) at Avalonia.Controls.Presenters.ScrollContentPresenter.<>c__DisplayClass38_0.<UpdateScrollableSubscription>b__3(Vector x) at System.Reactive.AnonymousSafeObserver
1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\AnonymousSafeObserver.cs:line 44
at System.Reactive.Sink1.ForwardOnNext(TTarget value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Internal\Sink.cs:line 50 at System.Reactive.Linq.ObservableImpl.Skip
1.Count._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\Skip.cs:line 59
at Avalonia.Reactive.LightweightObservableBase1.PublishNext(T value) at Avalonia.Reactive.AvaloniaPropertyObservable
1.PropertyChanged(Object sender, AvaloniaPropertyChangedEventArgs e)
at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
at Avalonia.Utilities.DeferredSetter1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty
1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotify(AvaloniaObject source, AvaloniaProperty
1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty1 property, T& field, T value) at Avalonia.Controls.Presenters.ScrollContentPresenter.set_Offset(Vector value) at Avalonia.Controls.Presenters.ScrollContentPresenter.<>c.<.cctor>b__12_8(ScrollContentPresenter o, Vector v) at Avalonia.DirectProperty
2.Avalonia.IDirectPropertyAccessor.SetValue(IAvaloniaObject instance, Object value)
at Avalonia.AvaloniaObject.<>c__DisplayClass54_0.<SetDirectValue>g__Set|0()
at Avalonia.AvaloniaObject.SetDirectValue(AvaloniaProperty property, Object value)
at Avalonia.AvaloniaObject.DirectBindingSubscription.OnNext(Object value)
at Avalonia.Reactive.SingleSubscriberObservableBase1.PublishNext(T value) at Avalonia.Data.TemplateBinding.PublishValue() at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority) at Avalonia.Utilities.DeferredSetter
1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.Utilities.DeferredSetter
1.SetAndNotify(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty
1 property, T& field, T value)
at Avalonia.Controls.ScrollViewer.set_Offset(Vector value)
at Avalonia.Controls.ScrollViewer.set_VerticalScrollBarValue(Double value)
at Avalonia.Controls.ScrollViewer.<>c.<.cctor>b__16_16(ScrollViewer o, Double v)
at Avalonia.DirectProperty2.Avalonia.IDirectPropertyAccessor.SetValue(IAvaloniaObject instance, Object value) at Avalonia.AvaloniaObject.<>c__DisplayClass54_0.<SetDirectValue>g__Set|0() at Avalonia.AvaloniaObject.SetDirectValue(AvaloniaProperty property, Object value) at Avalonia.AvaloniaObject.SetValue(AvaloniaProperty property, Object value, BindingPriority priority) at Avalonia.Data.TemplateBinding.System.IObserver<System.Object>.OnNext(Object value) at Avalonia.Reactive.LightweightObservableBase
1.PublishNext(T value)
at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
at Avalonia.Utilities.DeferredSetter1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty
1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotify(AvaloniaObject source, AvaloniaProperty
1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty1 property, T& field, T value) at Avalonia.Controls.Primitives.RangeBase.set_Value(Double value) at Avalonia.Controls.Primitives.RangeBase.<>c.<.cctor>b__28_5(RangeBase o, Double v) at Avalonia.DirectProperty
2.Avalonia.IDirectPropertyAccessor.SetValue(IAvaloniaObject instance, Object value)
at Avalonia.AvaloniaObject.<>c__DisplayClass54_0.<SetDirectValue>g__Set|0()
at Avalonia.AvaloniaObject.SetDirectValue(AvaloniaProperty property, Object value)
at Avalonia.AvaloniaObject.SetValue(AvaloniaProperty property, Object value, BindingPriority priority)
at Avalonia.Data.TemplateBinding.System.IObserver<System.Object>.OnNext(Object value)
at Avalonia.Reactive.LightweightObservableBase1.PublishNext(T value) at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority) at Avalonia.Utilities.DeferredSetter
1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.Utilities.DeferredSetter
1.SetAndNotify(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty
1 property, T& field, T value)
at Avalonia.Controls.Primitives.Track.set_Value(Double value)
at Avalonia.Controls.Primitives.Track.ThumbDragged(Object sender, VectorEventArgs e)
at Avalonia.Interactivity.Interactive.<AddHandler>g__InvokeAdapter|7_0[TEventArgs](Delegate baseHandler, Object sender, RoutedEventArgs args)
at Avalonia.Interactivity.Interactive.RaiseEventImpl(RoutedEventArgs e)
at Avalonia.Interactivity.Interactive.HierarchyTraverser2.Traverse(IInteractive target) at Avalonia.Interactivity.Interactive.BubbleEvent(RoutedEventArgs e) at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e) at Avalonia.Controls.Primitives.Thumb.OnPointerMoved(PointerEventArgs e) at System.Reactive.AnonymousObserver
1.OnNextCore(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\AnonymousObserver.cs:line 67
at System.Reactive.ObserverBase1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\ObserverBase.cs:line 36 at System.Reactive.Subjects.Subject
1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\Subject.cs:line 150
at Avalonia.Interactivity.Interactive.RaiseEventImpl(RoutedEventArgs e)
at Avalonia.Interactivity.Interactive.HierarchyTraverser2.Traverse(IInteractive target) at Avalonia.Interactivity.Interactive.BubbleEvent(RoutedEventArgs e) at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e) at Avalonia.Input.MouseDevice.MouseMove(IMouseDevice device, UInt64 timestamp, IInputRoot root, Point p, PointerPointProperties properties, KeyModifiers inputModifiers) at Avalonia.Input.MouseDevice.ProcessRawEvent(RawPointerEventArgs e) at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e) at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg) at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken) at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode) at TestPerf.Program.Main(String[] args) in C:\Users\axel\source\repos\TestPerf\TestPerf\Program.cs:line 13
My Code : XAML : ` <ListBox Items="{Binding MyItems1}" Width="800" x:Name="ControlBlc">
</ListBox>`
C# : `public MainWindow() { InitializeComponent(); #if DEBUG this.AttachDevTools(); #endif ListBox c = this.FindControl<ListBox>(“ControlBlc”); Bitmap r = new Bitmap(“aa.png”); ObservableCollection<Button> gt = new ObservableCollection< Button >(); for (int g = 0; g < 10000; g++) { Button t = new Button(); t.Background = new ImageBrush®; t.Width = 50; t.Height = 50;
gt.Add(t);
}
g = new ContextItem { MyItems1 = gt };
c.DataContext = g;
}
public class ContextItem
{
private ObservableCollection<Button> MyItems;
public ObservableCollection<Button> MyItems1
{
get
{
return MyItems;
}
set
{
MyItems = value;
}
}
}
public ContextItem g;`
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Virtualization can’t properly work with hardcoded control items instead of MVVM-like approach with item sources and templates. Setting “VirtualizationMode=“None”” fixes the problem. AFAIK it also is not reproduced with ItemsRepeater, so this PR should remove the problem https://github.com/AvaloniaUI/Avalonia/pull/4779. I probably wouldn’t expect fixing old virtualization in that case.
@maxkatz6 thanks for the reply.
I attempted using the styles as per your recommendation. However, the error persisted for me.
The gist of it is, I have the style you gave in the Window.Styles element. And later in the axaml I have the Menu and a few nested MenuItem elements. I’m using the menu to allow the user to toggle the app’s theme from Light to Dark. It will work on the first go. But if the user tries to toggle back, the app crashes on the MenuItem that is the parent of the MenuItems that toggle the theme.
I can show you my code setup. And I have the stacktrace; it is sliiiiightly different.
But I’m not sure if it’s appropriate to continue using this issue to discuss it here, or open a new issue. Which would you prefer?