Set ItemsSource of WPF ComboBox to Integer Collection in XAML

By Michael Detras

Usually, we bind the ItemsSource of an ItemsControl to some collection. If the type of the objects in the collection is not complex, say an integer, we can directly assign the collection to the ItemsSource via XAML.

Here is a simple example using a ComboBox.

<Window x:Class="WpfApplication1.MainWindow"
         
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         
xmlns:system="clr-namespace:System;assembly=mscorlib"
         
Title="MainWindow" Height="350" Width="525">
    
<StackPanel>
        
<ComboBox>
            <ComboBox.ItemsSource>
                 
<Int32Collection>
                     
<system:Int32>1</system:Int32>
                     
<system:Int32>2</system:Int32>
                 
</Int32Collection>
            </ComboBox.ItemsSource>
        
</ComboBox>
    
</StackPanel>
</Window>

Set ItemsSource of WPF ComboBox to Integer Collection in XAML  (2234 Views)