WPF - Listbox

ListBox is a control that provides a list of items to the user item selection. A user can select one or more items from the predefined list of items at a time. In a ListBox, multiple options are always visible to the user without any user interaction. The hierarchical inheritance of ListBox class is as follows −
Below are the commonly used Properties of ListBox class
Background
Gets or sets a brush that provides the background of the control. (Inherited from Control)
BorderThickness
Gets or sets the border thickness of a control. (Inherited from Control)
FontFamily
Gets or sets the font used to display text in the control. (Inherited from Control)
FontSize
Gets or sets the size of the text in this control. (Inherited from Control)
FontStyle
Gets or sets the style in which the text is rendered. (Inherited from Control)
FontWeight
Gets or sets the thickness of the specified font. (Inherited from Control)
Foreground
Gets or sets a brush that describes the foreground color. (Inherited from Control)
GroupStyle
Gets a collection of GroupStyle objects that define the appearance of each level of groups. (Inherited from ItemsControl)
Height
Gets or sets the suggested height of a FrameworkElement. (Inherited from FrameworkElement)
HorizontalAlignment
Gets or sets the horizontal alignment characteristics that are applied to a FrameworkElement when it is composed in a layout parent, such as a panel or items control. (Inherited from FrameworkElement)
IsEnabled
Gets or sets a value indicating whether the user can interact with the control. (Inherited from Control)
Item
Gets the collection used to generate the content of the control. (Inherited from ItemsControl)
ItemSource
Gets or sets an object source used to generate the content of the ItemsControl. (Inherited from ItemsControl)
Margin
Gets or sets the outer margin of a FrameworkElement. (Inherited from FrameworkElement)
Name
Gets or sets the identifying name of the object. When a XAML processor creates the object tree from XAML markup, run-time code can refer to the XAML declared object by this name. (Inherited from FrameworkElement)
Opacity
Gets or sets the degree of the object's opacity. (Inherited from UIElement)
SelectedIndex
Gets or sets the index of the selected item. (Inherited from Selector)
SelectedItem
Gets or sets the selected item. (Inherited from Selector)
SelectedValue
Gets or sets the value of the selected item, obtained by using the SelectedValuePath. (Inherited from Selector)
Style
Gets or sets an instance Style that is applied for this object during layout and rendering. (Inherited from FrameworkElement)
VerticalAlignment
Gets or sets the vertical alignment characteristics that are applied to a FrameworkElement when it is composed in a parent object such as a panel or items control. (Inherited from FrameworkElement)
Width
Gets or sets the width of a FrameworkElement. (Inherited from FrameworkElement)
DragEnter
Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement)
DragLeave
Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement)
DragOver
Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement)
DragStarting
Occurs when a drag operation is initiated. (Inherited from UIElement)
Drop
Occurs when the input system reports an underlying drop event with this element as the drop target. (Inherited from UIElement)
DropCompleted
Occurs when a drag-and-drop operation is ended. (Inherited from UIElement)
GotFocus
Occurs when a UIElement receives focus. (Inherited from UIElement)
IsEnabledChanged
Occurs when the IsEnabled property changes. (Inherited from Control)
KeyDown
Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement)
KeyUp
Occurs when a keyboard key is released while the UIElement has focus. (Inherited from UIElement)
LostFocus
Occurs when a UIElement loses focus. (Inherited from UIElement)
SelectionChanged
Occurs when the currently selected item changes. (Inherited from Selector)
SizeChanged
Occurs when either the ActualHeight or the ActualWidth property changes value on a FrameworkElement. (Inherited from FrameworkElement)
Arrange
Positions child objects and determines a size for a UIElement. Parent objects that implement custom layout for their child elements should call this method from their layout override implementations to form a recursive layout update. (Inherited from UIElement)
FindName
Retrieves an object that has the specified identifier name. (Inherited from FrameworkElement)
Focus
Attempts to set the focus on the control. (Inherited from Control)
GetValue
Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject)
IndexFromContainer
Returns the index to the item that has the specified, generated container. (Inherited from ItemsControl)
OnDragEnter
Called before the DragEnter event occurs. (Inherited from Control)
OnDragLeave
Called before the DragLeave event occurs. (Inherited from Control)
OnDragOver
Called before the DragOver event occurs. (Inherited from Control)
OnDrop
Called before the Drop event occurs. (Inherited from Control)
OnKeyDown
Called before the KeyDown event occurs. (Inherited from Control)
OnKeyUp
Called before the KeyUp event occurs. (Inherited from Control)
OnLostFocus
Called before the LostFocus event occurs. (Inherited from Control)
ReadLocalValue
Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject)
SetBinding
Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement)
SetValue
Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject)
Let’s create a new WPF project with the name WPFListBoxControl.
Let’s create a new WPF project with the name WPFListBoxControl.
Drag one list box and one textbox from the Toolbox.
Drag one list box and one textbox from the Toolbox.
When the user selects any item from the ListBox, it displayed on the TextBox as well.
When the user selects any item from the ListBox, it displayed on the TextBox as well.
Here is the XAML code in which a ListBox and a TextBox is created and initialized with some properties.
Here is the XAML code in which a ListBox and a TextBox is created and initialized with some properties.
<Window x:Class = "WPFListBoxControl.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:WPFListBoxControl"
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
	
   <Grid> 
      <ListBox Name = "listbox" Margin = "118,77,293,103">
         <ListBoxItem Content = "XAML Tutorials" /> 
         <ListBoxItem Content = "WPF Tutorials" /> 
         <ListBoxItem Content = "Silverlight Tutorials" /> 
         <ListBoxItem Content = "Windows 10 Tutorials" /> 
         <ListBoxItem Content = "iOS Tutorials" /> 
      </ListBox> 
		
      <TextBox Height = "23" x:Name = "textBox1" Width = "120" Margin = "361,116,0,0"  
         HorizontalAlignment = "Left" VerticalAlignment = "Top"  
         Text="{Binding SelectedItem.Content, ElementName=listbox}" /> 
   </Grid> 
	
</Window>
When the above code is compiled and executed, it will produce the following output −
We recommend that you execute the above example code and try the other properties and events of ListBox control.

    31 Lectures 
     2.5 hours 

 Anadi Sharma

    30 Lectures 
     2.5 hours 

 Taurius Litvinavicius
 Print
 Add Notes
 Bookmark this page