How to make custom control with ListBox?

前端 未结 1 344
傲寒
傲寒 2021-01-17 07:09

\"enter

I want to make horizontal ListBox with customized item template, so I make a b

1条回答
  •  自闭症患者
    2021-01-17 07:49

    Let's say we have a basic class named Item:

    public class Item : INotifyPropertyChanged
    {
        public string Text { get; set; } // Implement INotifyPropertyChanged 
        public string ImagePath { get; set; } // properly on these properties
    }
    

    And a collection of these in a view model:

    public ObservableCollection Items { get; set; } 
    

    Now to display these items in the UI, we use a ListBox and set the ItemsSource property:

    
    
    

    When it comes to defining the ListBox.ItemTemplate, you need to understand that this DataTemplate will be applied to each item and that it has access to all of the properties defined in the Item class:

    
        
            
                
                    
                    
                
            
        
    
    

    Therefore, you can access the properties in the collection class as shown above. You can find out the full story by looking at the ItemsControl.ItemTemplate Property page on MSDN.

    0 讨论(0)
提交回复
热议问题