2011-08-07 2 views
2

어떻게 든 목록 목록을 cavas 패널에 바인딩 할 수 있는지 궁금합니다. 예를 들어 "자체"가 "사각형"객체의 목록 인 객체 "레이어"가있는 경우. 레이어 목록을 캔버스에 바인딩 할 수 있습니까? 지금까지는 SelectMany 함수를 사용하여 중첩 목록의 평평하게 된 INumerable을 itemcontrol에 바인딩하여이 작업을 수행 할 수 있습니다. 그러나이 경우에는 "레이어"를 별도로 유지하고 직사각형의 Zindex를 유지하는 것이 좋습니다. 그것이있는 레이어는 레이어를 쉽게 재정렬 할 수 있습니다.중첩 목록을 WPF 캔버스 패널에 바인딩

나는 또한 itemcontrol을 중첩 시키려고했지만 예상대로 첫 번째 레이어 만 표시합니다. 목표는 캔버스의 모든 레이어에있는 모든 객체에 대해 사각형을 그려서 레이어 조작, 각 레이어에 새 객체 삽입 등을 허용하는 것입니다.

미리 감사드립니다! :)

답변

3

메인 아이템의 ItemTemplate을 만드는 방법 어떨까요? 다른 캔버스를 가지고 다른 아이템 컨트롤을 ItemsPanel으로 제어 하시겠습니까? (첫 번째 레이어 만 표시해야하는 이유는 표시되지 않습니다.)

사실 주 ItemsControl은 캔버스 일 필요는 없습니다. 그리드는 적어도 자신의 컬렉션이 자신의 좌표를 갖지 않는 한 Z- 주문, 그리드를 사용하는 경우 레이어 순서는 컬렉션의 모양과 동일합니다). (조금 자세한하지만 그것에 대해 아무것도 할 수 없습니다)

예 :

<ItemsControl ItemsSource="{Binding Data}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Grid /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <ItemsControl ItemsSource="{Binding LayerItems}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Canvas /> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemContainerStyle> 
        <Style TargetType="{x:Type ContentPresenter}"> 
         <!-- Here would the binding to some properties that take care of placement --> 
         <Setter Property="Canvas.Top" Value="{Binding Top}" /> 
         <Setter Property="Canvas.Left" Value="{Binding Left}" /> 
        </Style> 
       </ItemsControl.ItemContainerStyle> 
       <ItemsControl.ItemTemplate> 
        <!-- Some template for the individual items --> 
        <DataTemplate> 
         <Border Padding="5" BorderThickness="1" BorderBrush="Red" CornerRadius="3" 
           Background="White"> 
          <TextBlock Text="{Binding Name}" /> 
         </Border> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+0

감사합니다. 이것은 구현하려고 시도했던 초기 솔루션 이었지만 나는 잘못된 것을하고 있었다고 생각합니다. 이제 다시 작동합니다. 감사합니다. – ssaammuueell

+0

도움을 받아서 기쁩니다. :) –

0

처럼 H.B 내가 너무가 작동하지 않습니다 이유를 볼 수 없습니다 말했다. ItemsControl을 다른 ItemsContainer에두면 완벽하게 작동합니다.

<ItemsControl ItemsSource="{Binding Layers}"> 
    <ItemsControl.ItemsContainerStyle> 
    <Style> 
    <Setter Property="Template"> 
     <Setter.Value> 
     <ControlTemplate> 
     <ItemsControl ItemsSource="{Binding Rectangles}"/> 
     </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    </Style> 
    <ItemsControl.ItemsContainerStyle> 
</ItemsControl> 

그러나 ItemsContainerStyle을 수정할 필요없이, 당신의 항목에 대한 DataTemplate 내부의 하위 ItemsControl을 넣어 깨끗하다고 ​​생각. 나는 당신의 컨트롤이 Rectangles 바인딩에 필요한 모델의 속성에 대한 정보를 알아야 할 필요가 있다면 나쁜 디자인이라고 생각합니다.