在WPF中的ListView内填充ComboBox [英] Populating ComboBox inside ListView in WPF

查看:830
本文介绍了在WPF中的ListView内填充ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ListView内填充了ComboBox.屏幕截图如下

如上所示,它显示的是"M","a","c"而不是"Mac".为什么将单词分为字符?

在我编写的文件后面的代码中

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

            listView1.ItemsSource = dataTable.DefaultView;

在.xaml文件中,我写了:

<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>

我正在使用Visual Studio 2010

dataTable的屏幕截图,我用作ListView的ItemSource.(在调试过程中拍摄)

解决方案

ComboBox允许用户从多个项目中进行选择.它通过迭代ItemsSource中的所有项目并将每个项目添加到其Items中来填充自身.

您正在将ItemsSource设置为返回字符串的属性.由于可以迭代字符串,因此ComboBox会使用迭代时获得的项目填充自身,因此字符串"Mac"变为项目"M","a"和"c".

这就是为什么您看到自己所看到的.问题实际上是:您期望看到什么(或想要看到什么)以及为什么? ComboBox应该显示什么项目?如果您希望它显示出现在DataTable中的所有类别名称,则可以执行以下操作:

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

,然后使用DataTemplate从每个项目中拉出IC_Name列:

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

请注意,这样做会遇到各种各样的意外现象.例如,如果表中只有一行具有"Foo"作为值IC_Name,则当用户为该行选择其他值并且表被更新时,值"Foo"将从所有ComboBox es,使用户无法撤消该更改.另外,如果五行包含"Foo",则每个ComboBox都将在其下拉列表中显示"Foo"的所有五个实例.

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)

解决方案

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.

这篇关于在WPF中的ListView内填充ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆