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

查看:23
本文介绍了在 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" Horizo​​ntalAlignment="Left" Margin="23,12,0,0" Name="listView1" VerticalAlignment="Top" Width="447" ><网格视图>- - - - - - - - -- - - - - - - - -<GridViewColumn Header="类别名称" Width="150"><数据模板><ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120"/></GridViewColumn>- - - - - - - - - -- - - - - - - - - -</ListView.View>

我使用的是 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 列:

<数据模板><TextBlock Text="{绑定 IC_Name}"/></数据模板></ComboBox.ItemTemplate>

请注意,这样做会遇到各种意想不到的现象.例如,如果表中只有一行将Foo"作为 IC_Name 的值,当用户为该行选择其他值并且表被更新时,值Foo"将从所有 ComboBox 中消失,使用户无法撤消该更改.此外,如果五行包含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天全站免登陆