ItemsControl
에 컨트롤을 동적으로 추가하려고합니다. 내가 RadHubTile
컨트롤을 사용하고 있지만 이것은 어떤 컨트롤에도 적용된다고 생각합니다. 새 컨트롤을 추가 내 XAML은ItemsControl 요소는 이미 다른 요소의 자식입니다.
<ItemsControl x:Name="itemsControl" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<telerikPrimitives:RadUniformGrid x:Name="radUniformGrid" NumberOfColumns="3" NumberOfRows="3" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
잘 작동하고 올바르게 결합한다. 문제는 내가 페이지에서 빠져 나와 다시 돌아 왔을 때 발생합니다. 이 오류가 발생합니다
MS.Internal.WrappedException: Element is already the child of another element. ---> System.InvalidOperationException: Element is already the child of another element.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value)
at MS.Internal.XcpImports.Collection_AddDependencyObject[T] (PresentationFrameworkCollection`1 collection, DependencyObject value)
at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value)
at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value)
at System.Windows.PresentationFrameworkCollection`1.Add(T value)
at System.Windows.Controls.ItemsControl.AddVisualChild(Int32 index, DependencyObject container, Boolean needPrepareContainer)
at System.Windows.Controls.ItemsControl.AddContainers()
at System.Windows.Controls.ItemsControl.RecreateVisualChildren(IntPtr unmanagedObj)
--- End of inner exception stack trace ---
radHubTile 요소에 이미 부모가 있기 때문입니다. 아마도 내가 페이지에서 멀리 이동할 때 visualtree 또는 ItemsControl에서 제거해야합니까? OnBackKeyPress 뒤에있는 코드를 통해이 작업을 수행하려고했지만 정확히 어떻게이 작업을 수행 할 것인지 잘 모르겠습니다. 아니면 그게 문제를 해결할 수 있습니다.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
foreach (var item in itemsControl.Items)
{
UIElement uiElement =
(UIElement)itemsControl.ItemContainerGenerator.ContainerFromItem(item);
//this.itemsControl.Items.Remove(uiElement);
}
}
편집
위는 uiElement
{System.InvalidOperationException: Operation not supported on read-only collection.
어떤 제안을 제거하려고 시도 할 때 다음과 같은 오류를 준다? 감사.
와 함께. 감사합니다 – PutraKg