Live Chat Icon For mobile
Live Chat Icon

How do I bind the ListBox to a collection Property in my Window?

Platform: WPF| Category: ListBox

With a listbox like this defined in your XAML:

[XAML]
<ListBox x:Name='myLB' ></ListBox>

You can do so in your code-behind as follows:

[C#]
        public MyWindow()
        {
            InitializeComponent();

            Binding binding = new Binding();
            binding.Source = this;
            PropertyPath path = new PropertyPath('ListSource');
            binding.Path = path;
            // Setup binding:
            BindingOperations.SetBinding(this.myLB, ListBox.ItemsSourceProperty, binding);
        }

        public string[] ListSource
        {
            get { return new string[] { 'testing', 'test1' };}
        }

Share with

Share on twitter
Share on facebook
Share on linkedin