Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the control already has a visual parent Error #4265

Closed
axel578 opened this issue Jul 11, 2020 · 7 comments · Fixed by #9677
Closed

the control already has a visual parent Error #4265

axel578 opened this issue Jul 11, 2020 · 7 comments · Fixed by #9677

Comments

@axel578
Copy link

axel578 commented Jul 11, 2020

I 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.AvaloniaList1.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(ValueTuple2& backing, ValueTuple2 update) at Avalonia.PriorityValue.Avalonia.Utilities.ISetAndNotifyHandler<(System.Object,System.Int32)>.HandleSetAndNotify(AvaloniaProperty property, ValueTuple2& backing, ValueTuple2 value) at Avalonia.Utilities.DeferredSetter1.SetAndNotifyCallback[TValue](AvaloniaProperty property, ISetAndNotifyHandler1 setAndNotifyHandler, TValue& backing, TValue value) at Avalonia.PriorityValue.UpdateValue(Object value, Int32 priority) 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.AvaloniaObject.PriorityValueChanged(AvaloniaProperty property, Int32 priority, Object oldValue, Object newValue)
at Avalonia.PriorityValue.SetAndNotify(ValueTuple2& backing, ValueTuple2 update)
at Avalonia.PriorityValue.Avalonia.Utilities.ISetAndNotifyHandler<(System.Object,System.Int32)>.HandleSetAndNotify(AvaloniaProperty property, ValueTuple2& backing, ValueTuple2 value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotifyCallback[TValue](AvaloniaProperty property, ISetAndNotifyHandler1 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.AnonymousSafeObserver1.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.Skip1.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.AvaloniaPropertyObservable1.PropertyChanged(Object sender, AvaloniaPropertyChangedEventArgs e)
at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
at Avalonia.Utilities.DeferredSetter1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotify(AvaloniaObject source, AvaloniaProperty1 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.DirectProperty2.Avalonia.IDirectPropertyAccessor.SetValue(IAvaloniaObject instance, Object value)
at Avalonia.AvaloniaObject.<>c__DisplayClass54_0.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.DeferredSetter1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.Utilities.DeferredSetter1.SetAndNotify(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty1 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.LightweightObservableBase1.PublishNext(T value)
at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
at Avalonia.Utilities.DeferredSetter1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotify(AvaloniaObject source, AvaloniaProperty1 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.DirectProperty2.Avalonia.IDirectPropertyAccessor.SetValue(IAvaloniaObject instance, Object value)
at Avalonia.AvaloniaObject.<>c__DisplayClass54_0.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.DeferredSetter1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.Utilities.DeferredSetter1.SetAndNotify(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty1 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.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.AnonymousObserver1.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.Subject1.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>`

C# :
`public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
ListBox c = this.FindControl("ControlBlc");
Bitmap r = new Bitmap("aa.png");
ObservableCollection gt = new ObservableCollection< Button >();
for (int g = 0; g < 10000; g++)
{
Button t = new Button();
t.Background = new ImageBrush(r);
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;`

@Nhiqill
Copy link

Nhiqill commented Jul 16, 2020

I am experiencing this issue also. I'm on Windows 10, v0.10.0-preview1 (although I also tried 0.9.999-cibuild0008974 and it still happens). In my case, all of the items were added to the ListBox in XAML.
An unhandled exception of type 'System.InvalidOperationException' occurred in Avalonia.Visuals.dll
The control already has a visual parent.

The problem seems to be with ListBox scrolling, possibly item virtualization. For me:

  • 14 ListBoxItem(s) added in XAML fit exactly in the ListBox and do not involve any scrolling. Works fine.
  • Adding a 15th ListBoxItem triggers the display of the vertical scrollbar, and items can now be scrolled through vertically. Everything still works, even if you scroll up and down repeatedly.
  • Adding a 16th ListBoxItem causes the last entry to be displayed horizontally indented compared to the others, and scrolling down to the bottom and then up will 100% of the time crash the application with the before-mentioned error.
  • Setting the ListBox VirtualizationMode to "None" no longer crashes on scroll, even if I continue adding more items.

(Stacktrace)

@WakanaYuki
Copy link

I am experiencing this issue when resizing my Window.

  1. Change Window (800x450) to full screen size (not F11, just maximize)
  2. Change it back to normal size
  3. Crash (System.InvalidOperationException: The control already has a visual parent.)

@derekantrican
Copy link
Contributor

derekantrican commented Mar 12, 2021

I was also able to reproduce this very simply by just this code (Avalonia 0.10.0, .NET 5):

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Width="600" Height="450"
        x:Class="AvaloniaApp.MainWindow"
        Title="AvaloniaApp">
    <ListBox>
      <ListBoxItem>Item1</ListBoxItem>
      <ListBoxItem>Item2</ListBoxItem>
      <ListBoxItem>Item3</ListBoxItem>
      <ListBoxItem>Item4</ListBoxItem>
      <ListBoxItem>Item5</ListBoxItem>
      <ListBoxItem>Item6</ListBoxItem>
      <ListBoxItem>Item7</ListBoxItem>
      <ListBoxItem>Item8</ListBoxItem>
      <ListBoxItem>Item9</ListBoxItem>
      <ListBoxItem>Item10</ListBoxItem>
      <ListBoxItem>Item11</ListBoxItem>
      <ListBoxItem>Item12</ListBoxItem>
      <ListBoxItem>Item13</ListBoxItem>
      <ListBoxItem>Item14</ListBoxItem>
      <ListBoxItem>Item15</ListBoxItem>
    </ListBox>
</Window>

Just opening the application and scrolling the listbox throws this exception:
MswPCnkaJA

I literally started a new Avalonia project, added an example listbox (above), and hit an exception by just scrolling the listbox. Seems like a big deal.

Note: I found that the issue can be worked around by binding the Items property of the listbox rather than setting it in xaml. Then the exception does not seem to appear

@maxkatz6
Copy link
Member

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 #4779. I probably wouldn't expect fixing old virtualization in that case.

@General-Ridley
Copy link

General-Ridley commented Oct 27, 2022

@maxkatz6 I see your solution response, I know you suggested to set "VirtualizationMode="None"", however, I don't know where to put that. I attempted to put it in several places, but no element seemed to recognize the property. For reference, I'm getting the error on a MenuItem click.

**Edit**
I found another related issue. #5441 One to which you've also replied to. But that one was using a combobox. So I tried switching to using a combobox instead of a menuitem. However, despite literally copy/pasting the solution that the OP of that issue said worked for them. It did not work for me.

@maxkatz6
Copy link
Member

@General-Ridley same exception can't possibly happen if virtualization is disabled.
Do you have different stacktrace? Or maybe steps to reproduce are different?

To disable virtualization you need to change VirtualizationMode property on ItemsPresenter. ComboBox has a property for convenience that also changes virtualization mode of the item presenter inside.

I haven't tested, but something like this should change this mode for the menu item as well:

<Style Selector="MenuItem /template/ ItemsPresenter">
    <Setter Property="VirtualizationMode" Value="None" />
</Style>

@General-Ridley
Copy link

General-Ridley commented Nov 2, 2022

@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?

Kirpitchnsk added a commit to Kirpitchnsk/LogicalSimulator that referenced this issue May 14, 2023
Сильно переписал определение минимальных размеров элементов схемы.
Дописал VirtualizationMode="None" к каждому ListBox'у в качестве багофикса явного бага авалонии: AvaloniaUI/Avalonia#4265 Разумеется своим умом до такого решения я бы впринципе никогда в жизни бы не дошёл, без жёстких навыков дизассемблерщика кншн XD
Jomi404 added a commit to Jomi404/Curswork-Wiz that referenced this issue May 14, 2023
Сильно переписал определение минимальных размеров элементов схемы.
Дописал VirtualizationMode="None" к каждому ListBox'у в качестве багофикса явного бага авалонии: AvaloniaUI/Avalonia#4265 Разумеется своим умом до такого решения я бы впринципе никогда в жизни бы не дошёл, без жёстких навыков дизассемблерщика кншн XD
Diablox777 added a commit to Diablox777/VP2023RGR that referenced this issue May 14, 2023
Сильно переписал определение минимальных размеров элементов схемы.
Дописал VirtualizationMode="None" к каждому ListBox'у в качестве багофикса явного бага авалонии: AvaloniaUI/Avalonia#4265 Разумеется своим умом до такого решения я бы впринципе никогда в жизни бы не дошёл, без жёстких навыков дизассемблерщика кншн XD
Jomi404 added a commit to Jomi404/Curswork-Wiz that referenced this issue May 14, 2023
Сильно переписал определение минимальных размеров элементов схемы.
Дописал VirtualizationMode="None" к каждому ListBox'у в качестве багофикса явного бага авалонии: AvaloniaUI/Avalonia#4265 Разумеется своим умом до такого решения я бы впринципе никогда в жизни бы не дошёл, без жёстких навыков дизассемблерщика кншн XD
Kirpitchnsk added a commit to Kirpitchnsk/LogicalSimulator that referenced this issue May 14, 2023
Сильно переписал определение минимальных размеров элементов схемы.
Дописал VirtualizationMode="None" к каждому ListBox'у в качестве багофикса явного бага авалонии: AvaloniaUI/Avalonia#4265 Разумеется своим умом до такого решения я бы впринципе никогда в жизни бы не дошёл, без жёстких навыков дизассемблерщика кншн XD
Ivann03 added a commit to Ivann03/logic_element_simulator that referenced this issue May 23, 2023
Сильно переписал определение минимальных размеров элементов схемы.
Дописал VirtualizationMode="None" к каждому ListBox'у в качестве багофикса явного бага авалонии: AvaloniaUI/Avalonia#4265 Разумеется своим умом до такого решения я бы впринципе никогда в жизни бы не дошёл, без жёстких навыков дизассемблерщика кншн XD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants