Bind to Count of items in the DataContext

后端 未结 5 1236
别跟我提以往
别跟我提以往 2021-02-12 03:45

I want to bind to the Count/amount of items within my DataContext.

I have an object, lets say person which has a List

as a property. I would
5条回答
  •  别那么骄傲
    2021-02-12 04:21

    To expand on tehMick's answer with functional sample code:

    XAML:

    
        
            
                
                    
                        
                            
                                
                                
                            
                            
                            
                                 addresses
                            
                        
                    
                
            
        
    
    

    Code Behind:

    namespace Sandbox.Wpf.PropertyCount
    {
        /// 
        /// Interaction logic for PropertyCount.xaml
        /// 
        public partial class PropertyCount : Window
        {
    
            public PropertyCount()
            {
                InitializeComponent();
                this.DataContext = new Model();
            }
        }
    
        public class Model
        {
            public List People { get; private set; }
    
            public Model()
            {
    
                People = new List{
                    new Person ("joe", new List { 1, 2, 3 }),
                    new Person ("bob", new List { 1, 2 }),
                    new Person ("kay", new List { 1, 2, 3, 4, 5 }),
                    new Person ("jill", new List { 1, 2, 3, 4 }),
                };
            }
        }
    
        public class Person
        {
            public string Name { get; set; }
            public List Addresses { get; set; }
    
            public Person(string name, List addresses)
            {
                Name = name;
                Addresses = addresses;
            }
        }
    
    }
    
        

    提交回复
    热议问题