Wpf – Populating ComboBox inside ListView in WPF

comboboxlistviewwpfwpf-controls

I have populated a ComboBox inside a ListView. Screen shot is given below
enter image description here

As shown above it's displaying "M", "a", "c" instead of "Mac". Why it is separating the word into characters?

In code behind file I've written

ItemCategoryDAL itemCategoryDalObj = new ItemCategoryDAL();
            DataTable dataTable = itemCategoryDalObj.GetAllItemCategory();

            listView1.ItemsSource = dataTable.DefaultView;

And in .xaml file I've written:

<ListView  Height="148" HorizontalAlignment="Left" Margin="23,12,0,0" Name="listView1" VerticalAlignment="Top" Width="447" >
  <ListView.View>
     <GridView>
        - - - - - - - - 
        - - - - - - - -
        <GridViewColumn Header="Category Name" Width="150">
           <GridViewColumn.CellTemplate>
               <DataTemplate>
                  <ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" />
               </DataTemplate>
           </GridViewColumn.CellTemplate>
        </GridViewColumn> 
        - - - - - - - - -
        - - - - - - - - -
     </GridView>
  </ListView.View>
</ListView>

I'm using Visual Studio 2010

Screen shot of dataTable which I used as ItemSource for the ListView.(Taken during debuging)
enter image description here

Best Solution

A ComboBox allows the user to select from multiple items. It populates itself by iterating through all of the items in its ItemsSource and adding each item to its Items.

You're setting ItemsSource to a property that returns a string. Since a string can be iterated over, the ComboBox is populating itself with the items it gets when iterating over it, so the string "Mac" turns into the items "M", "a", and "c".

That's why you're seeing what you're seeing. The question really is: what did you expect to see (or what do you want to see) and why? What items should the ComboBox be displaying? If you want it to display all of the category names that appear in the DataTable, you could do something like:

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ItemsSource}"

and then pull out the IC_Name column from each item using a DataTemplate:

<ComboBox.ItemTemplate>
   <DataTemplate>
      <TextBlock Text="{Binding IC_Name}"/>
   </DataTemplate>
</ComboBox.ItemTemplate>

Note that there are going to be all kinds of unexpected phenomena that you encounter by doing this. For instance, if only one row in the table has "Foo" as a value of IC_Name, the moment the user selects some other value for that row and the table gets updated, the value "Foo" will disappear from all of the ComboBoxes, making it impossible for the user to undo that change. Also, if five rows contain "Foo", each ComboBox will display all five instances of "Foo" in their dropdown.