2013-05-24 4 views
1

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. 

어떤 제안을 제거하려고 시도 할 때 다음과 같은 오류를 준다? 감사.

답변

0

ItemsControl에 UI 요소 모음 (RadHubTile)을 바인딩하는 것 같습니다. 이 작업을 수행하는 대신 ItemsControlItems 컬렉션의 타일을 직접 추가하거나 타일의 데이터 (비 UI) 개체 모음을 ItemsSource에 바인딩 한 다음 ItemTemplate을 사용하여 RadHubTile 컨트롤 자체를 선언해야합니다. 새로운 ItemsControl 인스턴스가 생성 될 때마다 새 인스턴스로 생성됩니다.

+0

와 함께. 감사합니다 – PutraKg

0
+0

'{System.InvalidOperationException : 작업이 읽기 전용 모음에서 지원되지 않습니다 .'. ItemsControl 항목은 바인딩을 통해 추가 할 때만 읽습니다. – PutraKg

+0

바인딩 된 목록을 지우고 다시 채우시겠습니까? –

+0

나는 그것을 시험해 보았다. 그러나 내가 지울 수있는 유일한 방법은 그리드에서'ItemsControl'을 제거하는 것입니다. 그런 다음 페이지를 다시 추가해야하므로 다른 문제가 발생합니다. XAML을 통해 코드 뒤에 숨기고 싶지 않습니다. – PutraKg

관련 문제