在 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
的屏幕截图,我将其用作 ItemSource
的ListView。(调试时拍摄)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ComboBox
允许用户从多个项目中进行选择。它通过迭代其ItemsSource
中的所有项目并将每个项目添加到其Items
中来填充自身。您将
ItemsSource
设置为返回字符串的属性。由于可以迭代字符串,因此ComboBox
会使用迭代时获得的项目填充自身,因此字符串“Mac”会变成项目“M”、“a”和“ c”。这就是为什么你会看到你所看到的。问题实际上是:您期望看到什么(或者您想看到什么)以及为什么?
ComboBox
应该显示哪些项目?如果您希望它显示DataTable
中出现的所有类别名称,您可以执行以下操作:然后使用 < code>DataTemplate:
请注意,这样做可能会遇到各种意想不到的现象。例如,如果表中只有一行将“Foo”作为
IC_Name
的值,则当用户为该行选择其他值并且表更新时,值“Foo”将从所有 ComboBox 中消失,使用户无法撤消该更改。此外,如果五行包含“Foo”,则每个ComboBox
将在其下拉列表中显示“Foo”的所有五个实例。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.绑定似乎有效,IC_NAME 存在并返回“Mac”作为字符串。这将隐式转换为具有三个条目的可枚举字符串:“M”、“a”和“c”。这就是 ComboBox 的 ItemsSource。
也许应该是这样的:
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: