Tow*_*hid 4 wpf listview combobox wpf-controls
我在 aComboBox内填充了a ListView。屏幕截图如下

如上所示,它显示“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>
<网格视图>
- - - - - - - - -
- - - - - - - - -
<GridViewColumn Header="类别名称" Width="150">
<GridViewColumn.CellTemplate>
<数据模板>
<ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" />
</数据模板>
</GridViewColumn.CellTemplate>
</GridViewColumn>
- - - - - - - - - -
- - - - - - - - - -
</网格视图>
</ListView.View>
</列表视图>
我正在使用 Visual Studio 2010
dataTable我用作ItemSourceListView 的屏幕截图。(在调试期间拍摄)

AComboBox允许用户从多个项目中进行选择。它通过遍历其中的所有项目ItemsSource并将每个项目添加到其Items.
您正在设置ItemsSource一个返回字符串的属性。由于字符串可以迭代ComboBox,所以它会用迭代时得到的项目填充自身,因此字符串“Mac”变成了项目“M”、“a”和“c”。
这就是为什么你会看到你所看到的。真正的问题是:您希望看到什么(或您想看到什么)以及为什么?哪些项目应该在ComboBox被显示?如果您希望它显示出现在 中的所有类别名称DataTable,您可以执行以下操作:
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ItemsSource}"
Run Code Online (Sandbox Code Playgroud)
然后IC_Name使用 a 从每个项目中拉出列DataTemplate:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding IC_Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
请注意,这样做会遇到各种意想不到的现象。例如,如果表中只有一行的值是“Foo” IC_Name,当用户为该行选择其他值并且表被更新时,值“Foo”将从所有ComboBoxes 中消失,使得用户无法撤消该更改。此外,如果五行包含“Foo”,则每行都ComboBox将在其下拉列表中显示“Foo”的所有五个实例。
| 归档时间: |
|
| 查看次数: |
16458 次 |
| 最近记录: |