2012-01-18 5 views
0

저는 Silverlight에서 Prism 4를 사용하고 있고 ItemsControl을 사용하여 여러보기를 호스팅하려고합니다. 나는 모든 뷰가 지정된 ItemTemplate 안에 싸이게하거나 ItemStyle을 지정하여 Silverlight Toolkit의 Expander 컨트롤과 같은 것을 사용할 수있게하고 싶습니다. ItemTemplate을 지정하려고하면 처리되지 않은 System.NotSupportedException이 런타임에 throw됩니다.ItemsControl에 대한 TabControlRegionAdapter.ItemContainerStyle Attached Property와 비슷한 것이 있습니까?

ItemsControl.Items must not be a UIElement type when an ItemTemplate is set. 
    at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.GetContainerForItem(Object item, DependencyObject recycledContainer) 
    at System.Windows.Controls.ItemContainerGenerator.Generator.GenerateNext(Boolean stopAtRealized, Boolean& isNewlyRealized) 
    at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.GenerateNext(Boolean& isNewlyRealized) 
    at System.Windows.Controls.ItemsControl.AddContainers() 
    at System.Windows.Controls.ItemsControl.RecreateVisualChildren(IntPtr unmanagedObj) 

코드

<ItemsControl Regions:RegionManager.RegionName="DetailsRegion"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border BorderBrush="Red" BorderThickness="1"> 
       <ContentPresenter Content="{Binding}"/> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    </ItemsControl> 
+0

당신은 어떻게 ItemsSource를 설정하는에게 도움이? 예외를 기반으로 명시 적으로 ItemsControl (itemControl.itemsControl.Items.Add())에 ItemsSource를 설정하는 대신 Binding을 통해 명시 적으로 항목을 추가하는 것처럼 들리거나 Codebehind (itemscontrol.ItemsSource = myCollection). –

+0

Xaml에서 ItemsSource를 설정하지 않습니다. Prism4에서 기본 ItemsControlRegionAdapter를 사용하고 있습니다. ItemsSource를 void Adapt (IRegion region, ItemsControl regionTarget)에 this : regionTarget.ItemsSource = region.Views로 설정합니다. 뷰는 IViewsCollection : IEnumerable , INotifyCollectionChanged입니다. 너무 미친 여기에가는 아무것도 ... – JJS

+0

컬렉션에 무엇이 있는지 알고 있니? SL은 이미 DataTemplate의 DataContext로 사용하는 대신 렌더링하려고 시도 할 수있는 요소라는 것을 SL이 알고 있기 때문에 문제가 될 것이라고 생각합니다. –

답변

0

내가 PRISM을 사용하기 때문에 그것은 잠시왔다,하지만 다음 컬렉션에 추가하기 전에 요소를 랩하는 사용자 정의 IRegion을 구현하는 데 사용할 수있는 예입니다 .

public class RegionWrapper : Region 
{ 
    public override Microsoft.Practices.Composite.Regions.IRegionManager Add(object view, string viewName, bool createRegionManagerScope) 
    { 
     var myWrapper = new Wrapper(); 
     myWrapper.Content = view; 
     return base.Add(myWrapper, viewName, createRegionManagerScope); 
    } 
} 

방금 ​​물론

protected override RegionAdapterMappings ConfigureRegionAdapterMappings() 
    { 
     var regionAdapterMappings = base.ConfigureRegionAdapterMappings(); 
     regionAdapterMappings.RegisterMapping(typeof(ItemsControl), Container.Resolve<RegionWrapperAdapter>()); 
     return regionAdapterMappings; 
    } 

어댑터 등록 PRISM에 그들은 당신의 부트 스트랩에 다음 어댑터를

public class RegionWrapperAdapter : RegionAdapterBase<IRegionAdapter> 
{ 
    protected override Microsoft.Practices.Composite.Regions.IRegion CreateRegion() 
    { 
     return new RegionWrapper(); 
    } 
} 

부르는 지역 공장을 만들 필요가이 항목을 등록하려면 나머지 부분은 컨트롤 '래퍼'를 구현하여 해당 클래스를 만들고 내용을 추가 할 수 있도록하는 것입니다. 이 예제에서와 비슷한 특정 스타일의 ContentControl 일 수도 있고 더 멋진 것을 추가 할 수도 있습니다.

이 코드는 PRISM의 이전 버전을 기반으로하므로 최근에 상황이 변경되었을 수 있습니다.

희망이

미구엘

+0

@Miguel Madero에게 감사드립니다. 나는 이것을 시도하고 다시 당신에게 돌아갈거야. – JJS