WPF ListBox Binding Object


This entry is part 3 of 6 in the series WPF ListBox

This post has the C# code for a WPF project that has a ListBox with a list of fruit, a simple TextBlock that displays the fruit that the user clicked on (selected), and a button that when clicked adds a “jackfruit” to the list and refreshes the list. Instead of, as in the previous post using the event, let’s use a Binding object in the MainWindow’s constructor. Have a look at Adam Nathan’s book WPF 4.5 Unleashed on page 362 (in chapter 13) if you have access to that book. If you don’t have the book that’s fine as I will include the code here. I created a WPF project in Visual Studio Community called ListBoxBindArray. This means that we have a list box that has data that originally comes from an array that uses data binding to a text block to show which fruit was selected.

We have a series of posts on Data Binding starting with the post called WPF Data Binding Introduction.

Here is the XAML code. Feel free to use this code in your own project.

<Window x:Class="ListBoxBindArray.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListBoxBindArray"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Loaded="Window_Loaded"
        Title="ListBoxBindArray" Height="350" Width="500">
    <StackPanel Orientation="Horizontal" Margin="14">
        <StackPanel Orientation="Vertical">
            <Label>ListBox</Label>
            <ListBox x:Name="lstbx" Height="200" Width="200"></ListBox>
        </StackPanel>
        <TextBlock x:Name="txtblck" Margin="10 " Height="20" Width="100"></TextBlock>
        <Button Click="Button_Click" Height="35">Add Jackfruit</Button>
    </StackPanel>
</Window>

Here is the code behind.

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace ListBoxBindArray
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Binding binding = new Binding();
            binding.Source = lstbx;
            binding.Path = new PropertyPath("SelectedItem");
            txtblck.SetBinding(TextBlock.TextProperty, binding);
        }
        public void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string[] fruit_array = { "apple", "peach", "mango" };
            // convert an array to a list:
            List<string> lst = new List<string>(fruit_array);
            lst.Add("banana");
            lstbx.ItemsSource = lst;
            lst.Add("pear");
            lstbx.ItemsSource = null;
            lstbx.ItemsSource = lst;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var lst = lstbx.Items.Cast<string>().ToList();  // Linq
            lst.Add("jackfruit");
            lstbx.ItemsSource = null;
            lstbx.ItemsSource = lst;
        }
    }
}
Series Navigation<< WPF ListBox SelectionWPF ListBox Control >>