2012-10-09 4 views
4

XAML의 DataTemplate을 중첩 클래스와 연결할 수 있습니까?DataTemplate을 중첩 클래스에 묶을 수 있습니까?

MVVM 응용 프로그램에서 작업 중이며 데이터 템플릿 문제가 발생했습니다. 나는 아이템 컨트롤을위한 다른 뷰 모델 모음을 제공하는 뷰 모델을 가지고있다. 이러한 항목은 외부 뷰 모델에서 중첩 된 클래스로 정의 된 계층의 일부입니다. 지금까지 XAML에서 내부 중첩 클래스를 참조하는 매핑을 만들 수 없었습니다. 여기

은 (간결 간체) 클래스 계층 구조입니다 : XAML에서

public class MainViewModel 
{ 
    public class A 
    { 
    } 

    public class B : A 
    { 
    } 

    public class C : A 
    { 
    } 

    public ObservableCollection<A> Items 
    { 
     get; 
     set; 
    } 
} 

, 나는 유형 B와 C에 DataTemplate을지도하기 위해 노력하고있어,하지만 난 완전히 중첩 된 클래스 이름을 규정 할 수 없다.

<ItemsControl ItemsSource="{Binding Path=Items}"> 
    <ItemsControl.Resources> 
     <DataTemplate DataType="{x:Type ns:BracingViewModel.B}"> 
      <Grid> 
      .... 
      </Grid> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type ns:BracingViewModel.C}"> 
      <Grid> 
      .... 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.Resources> 
</ItemsControl> 

문제 : 중첩 된 클래스에 대한 참조가 XAML의 빌드 오류로 표시됩니다.

Error 5 Cannot find the type 'ns:B'. Note that type names are case sensitive. Line... 

Error 5 Cannot find the type 'ns:C'. Note that type names are case sensitive. Line... 

내가 MainViewModel 클래스 (즉, 네임 스페이스 수준) 외부의 A, B, C 클래스 계층 구조를 이동하는 경우,이 잘 작동 : 나는 다음과 같은 얻을.

일반적인 습관으로, 뷰 모델과 관련된 클래스를 그 안에 중첩 된 클래스로 정의하려고하지만이 문제는 나를이 문제로 안내합니다.

내 질문 : DataTemplate을 중첩 클래스와 연결할 수도 있습니까? 그렇다면 XAML 부분에서 어떻게 처리됩니까? 사전에

감사합니다 ... 조

답변

17

이 나를 위해 작동합니다 : 즉

<ItemsControl ItemsSource="{Binding Path=Items}"> 
     <ItemsControl.Resources> 
      <DataTemplate DataType="{x:Type ns:MainViewModel+B}"> 
       <Grid Background="Blue" 
         Width="30" 
         Height="30"> 

       </Grid> 
      </DataTemplate> 
      <DataTemplate DataType="{x:Type ns:MainViewModel+C}"> 
       <Grid Background="Chartreuse" Width="30" Height="30"> 

       </Grid> 
      </DataTemplate> 
     </ItemsControl.Resources> 
    </ItemsControl> 

, 단지 x:Type 태그 확장

신용에 +.을 변경 : this thread

+0

매력처럼 작동하지만, 즉시 WP20 디자인 문제로 인해 WP20이 발생했습니다. 이것이 사용될 때 양식을 표시 할 수 없습니다. 하지만 바인딩이 제대로 작동했습니다. –

+0

신의 땅에서이 구문을 이해할 것으로 예상되는 사람은 누구입니까? Microsoft가 API를 설계하고 구현하는 것을 금지하는 법률이 있어야합니다. –