2010-08-10 3 views
0

Notes 목록을 표시하려고합니다. NoteViewModel 컬렉션에 바인딩 된 ItemsControl이 있습니다. 그래서 Items 컨트롤의 데이터 템플릿에서 NoteControl (노트 표시를위한 사용자 정의 컨트롤)을 만들고 ViewModel 속성을 컬렉션의 NoteViewModel에 바인딩하려고합니다.Silverlight에서 UserControls에 내부 ViewModels 연결

나는 현재이 있습니다

<ItemsControl x:Name="itemsControl1" Grid.Row="1" ItemsSource="{Binding Notes}" > 
     <ItemsControl.Template> 
      <ControlTemplate TargetType="ItemsControl"> 
       <ScrollViewer> 
        <ItemsPresenter/> 
       </ScrollViewer> 
      </ControlTemplate> 
     </ItemsControl.Template> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <uc:NoteControl uc:NoteControl.ViewModel="{Binding}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

를 그러나 나는이 예외 받고 있어요 :

System.ArgumentException: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'NotePrototype.NoteViewModel'. 

이를 배선에 대한 적절한 구문은 무엇입니까? 내부 뷰 모델을 동적으로 생성/바인딩 된 내부 UserControls에 연결하는 더 나은 기술이 있습니까? 그건 당신이

추가 메모를 ViewModel이되기 때문에

답변

1

당신의 DataContext에 UserControl을로하고 뷰 모델 속성을 필요로하지 않는 UserControl을의 뷰 모델을 부착하는 것이 좋습니다, 당신은 단지 암시 적 데이터 컨텍스트에 바인딩 할 수 있습니다 : 당신이 아래의 예를 따라 디자이너에서 데이터 바인딩을 사용하려면이 :

<UserControl 
      <!-- 
       all the other declaration needed are here 
      --> 
     xmlns:local="clr-namespace:NotePrototype" 
     d:DataContext="{DynamicResource ViewModel}" 
> 
    <UserControl.Resources> 
     <local:NoteViewModel x:Key="ViewModel" d:IsDataSource="True" /> 
    </UserControl.Resources> 

<!-- 
    put your content here 
--> 
</UserControl> 

ItemsControl에 대한 예를 편집 :

<ItemsControl x:Name="itemsControl1" Grid.Row="1" ItemsSource="{Binding Notes}" > 
    <ItemsControl.Template> 
     <ControlTemplate TargetType="ItemsControl"> 
      <ScrollViewer> 
       <ItemsPresenter/> 
      </ScrollViewer> 
     </ControlTemplate> 
    </ItemsControl.Template> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <uc:NoteControl DataContext="{Binding}"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+0

나는 이것을 얻지 못할까 봐 걱정된다. XAML은 ItemsControl의 ItemTemplate에서 어떤 모습입니까? – RationalGeek

+0

감사합니다! 이제는 의미가 있습니다. :-) – RationalGeek