How do I bind a string array with a ListBox?

Platform: WPF| Category: ListBox

A ListBox can be bound with a string array using the ItemsSource property of the ListBox control. The following code snippet is used to bind a string array with the ListBox.

[XAML]

<Window x:Class='WpfApplication4.Window1'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    Title='Window1' Height='300' Width='300' Loaded='Window_Loaded'>
    <Grid>
       <ListBox Name='LB2'/>
    </Grid>
</Window>
[C#]

private void Window_Loaded(object sender, RoutedEventArgs e)
{
string[] lbitems = { 'Item1', 'Item2', 'Item3', 'Item4' };
      LB2.ItemsSource = lbitems;
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.