The Listbox in windows form contains the text value only. In ListBox, we can’t display the list of the images with the text.
Example of the ListBox
The ListBox class in C# and WPF shows the Listbox control. ListBox in WPF has contained the collection of the items in the ListBox. Here we will show how to add the items, remove the item from the Listbox, and bind the Listbox to the data source.
The height and width properties will show us the width and height of the Listbox. The name property of the ListBox will show us the name of the control, for the control name is known as the unique identifier. Margin property of the ListBox shows us the margin of the Listbox on the parent control. Horizontal and VerticalAlignment properties of ListBox are used to set the horizontal and vertical alignment.
The below code is used to set the name, height, and width of the ListBox Control.
Here we will perform the different functions on the Listbox.
Statically addition of the item to the ListBox Control
ListBox control is a collection of the ListBox item. To add the item in the ListBox control, we will write the following code:
To add the item in the ListBox control in WPF, we will follow the following steps:
For the WPF application, click on File->New->Project as shown in the below screenshot:
When you click on the project, following window will appear as shown in the below screenshot:
As per the above screenshot, click on Visual C#->WPF App (.NET Framework)->Name of App (WPF_App1)->Click OK.
After this, the following window will appear as shown in the below screenshot:
To add the ListBox control statically, we will write the following code in the MainWindow.XAML window:
Design of the above code is as shown below:
In the above code, we add the item in the ListBox at the time of the design from XAML.
Adding ListBox Dynamically in WPF
Here we will show how to add the items in the listbox from the code. For this we will change the UI and add the textbox and the button control on the page.
To add the TextBox and Button, we will write the following XAML code:
After clicking on the button event handler, we will add the contents of the textbox to the listbox after calling the ListBox.Items.Add method.
For this we will write the following code:
Formatting the ListBox item
The Foreground and Background attributes of the ListBoxItem shows the background and foreground colors of the item. To set the background and foreground color, we will write the following code for the ListBox item.
For the formatting of the listbox, we will write the following code.
Design View of the above code
The output of the above code is as shown below:
Loading of Image in ListBox
Loading of the image in the ListBox is one of the advantages of the WPF, which is not supported by the Windows Form.
We can put any control in the WPF in the ListBoxItem like image and text. If we want to display the images with the text, we have to keep the text block and image control within the StackPanel. The Image. Source property contains the name of the image which we want to display in the image control. The TextBlock.Text property takes the string, which we want to show in the TextBlock.
Here we have a code through which we will show the text and image to the ListBoxItem.
CheckBox in ListBox
If we keep the checkbox control inside the ListBoxItems, here we will generate the ListBox control which contains the checkbox in it. CheckBox will host the controls. Image and text block can be used as a content of the checkbox in the form of the object.
Here we will change the code of the ListBoxItems and add the CheckBoxes to the items. The property Name will use to name of the checkbox. CheckBox can be accessed by using the Name property.
Here we have created the listbox control and apply the different method to show the different functionalities of the WPF listbox control. WPF listbox control is a collection of the itemcontrol in it.