I have populated a ComboBox
inside a ListView
. Screen shot is given below
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)
The binding seems to work, IC_NAME exists and returns "Mac" as a string. This will implicitely converted to an enumerable of string with three entries: "M", "a" and "c". And this is then the ItemsSource of your ComboBox.
Probably it should be something like:
A
ComboBox
allows the user to select from multiple items. It populates itself by iterating through all of the items in itsItemsSource
and adding each item to itsItems
.You're setting
ItemsSource
to a property that returns a string. Since a string can be iterated over, theComboBox
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 theDataTable
, you could do something like:and then pull out the
IC_Name
column from each item using aDataTemplate
: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 theComboBox
es, making it impossible for the user to undo that change. Also, if five rows contain "Foo", eachComboBox
will display all five instances of "Foo" in their dropdown.