Populating ComboBox inside ListView in WPF

2020-03-04 09:15发布

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

2条回答
乱世女痞
2楼-- · 2020-03-04 09:36

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.

<ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" />

Probably it should be something like:

<ComboBox 
  SelectedItem="{Binding Path=IC_NAME}"
  ItemsSource="{Binding Path=NameOfACollectionProperty }" 
  Width="120" />
查看更多
神经病院院长
3楼-- · 2020-03-04 09:45

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.

查看更多
登录 后发表回答