UserControl as DataTemplate inside ListBox

后端 未结 4 1854
北海茫月
北海茫月 2020-12-25 13:48

I want to reuse my UserControls in other UserControls like page or window as DataTemplates, in this example inside a ListBox. Everything is MVVM.

I\'ve a UserControl

4条回答
  •  抹茶落季
    2020-12-25 14:15

    For the ListBox control an inferred ListBoxItem is created for each item in the items source. The Item is set as the DataContext and your ItemTemplate is set as the template. Since DataContext inherits, you don't have to explicitly set it because it's already the instance of Card in your DataTemplate.

    For this case, you don't have to set the DC on the CardControl because it's set for you.

     
       
         
           
    
             
             // would display ID of Card 
             // would display Image of Card 
    
             
             
    
           
         
       
     
    

    Should work for you.

提交回复
热议问题