在WPF的ListView中填充组合框
我在在WPF的ListView中填充组合框,wpf,listview,combobox,wpf-controls,Wpf,Listview,Combobox,Wpf Controls,我在列表视图中填充了一个组合框。屏幕截图如下所示 如上图所示,它显示的是“M”、“a”、“c”而不是“Mac”。为什么要把这个词分成几个字符 在我编写的代码隐藏文件中 ItemCategoryDAL itemCategoryDalObj = new ItemCategoryDAL(); DataTable dataTable = itemCategoryDalObj.GetAllItemCategory(); listView1.ItemsSo
列表视图中填充了一个组合框
。屏幕截图如下所示
如上图所示,它显示的是“M”、“a”、“c”而不是“Mac”。为什么要把这个词分成几个字符
在我编写的代码隐藏文件中
ItemCategoryDAL itemCategoryDalObj = new ItemCategoryDAL();
DataTable dataTable = itemCategoryDalObj.GetAllItemCategory();
listView1.ItemsSource = dataTable.DefaultView;
ItemCategoryDAL itemCategoryDalObj=新的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>
- - - - - - - -
- - - - - - - -
- - - - - - - - -
- - - - - - - - -
我正在使用VisualStudio2010
dataTable
的屏幕截图,我将其用作列表视图的ItemSource
。(在调试期间拍摄)
绑定似乎正常,IC_名称存在并以字符串形式返回“Mac”。这将隐式转换为一个包含三个条目的可枚举字符串:“M”、“a”和“c”。这就是您的组合框的ItemsSource
<ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" />
可能应该是这样的:
<ComboBox
SelectedItem="{Binding Path=IC_NAME}"
ItemsSource="{Binding Path=NameOfACollectionProperty }"
Width="120" />
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ItemsSource}"
A组合框
允许用户从多个项目中进行选择。它通过迭代其itemsource
中的所有项并将每个项添加到其项中来填充自身
您正在将ItemsSource
设置为返回字符串的属性。由于字符串可以被迭代,因此当对其进行迭代时,组合框将用它得到的项填充自己,因此字符串“Mac”将变成项“M”、“a”和“c”
这就是为什么你看到了你所看到的。问题是:你期望看到什么(或者你想看到什么)以及为什么?组合框应显示哪些项目?如果希望它显示数据表中出现的所有类别名称,可以执行以下操作:
<ComboBox
SelectedItem="{Binding Path=IC_NAME}"
ItemsSource="{Binding Path=NameOfACollectionProperty }"
Width="120" />
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ItemsSource}"
然后使用DataTemplate
从每个项目中拉出IC\u Name
列:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding IC_Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
请注意,这样做会遇到各种各样的意外现象。例如,如果表中只有一行具有“Foo”作为IC_Name
的值,那么当用户为该行选择其他值并且表得到更新时,值“Foo”将从所有组合框中消失,这使得用户无法撤消该更改。此外,如果五行包含“Foo”,则每个组合框的下拉列表中将显示“Foo”的所有五个实例。IC\u NAME
是一个DataTable
列名。@David Webb:IC\u NAME可能返回一个字符串。这就是为什么你会看到这个结果。我假设这是您希望在下拉列表中选择的值,而不是ItemsSource。使用ItemsSource,您可以指定生成项目的集合。实际上,您所说的集合属性的名称是什么意思?(可能是我不知道,因为我缺乏知识。)必须定义组合框的项。可以通过在标记中添加ComboBoxItem元素或指定ItemsSource来实现这一点。我假设IC_NAME列包含应该选择的值。这是通过SelectedItem或SelectedValue属性声明的。但是,如果是这种情况,还必须指定组合框中应包含哪些项。但是我没有足够的关于你的应用程序的信息来说明哪些是项目。请看这里了解更多信息。到itemsource:我正在使用dataTable
作为列表视图的ItemSorce
。并使用此数据表的IC\u NAME
列填充组合框。在编辑的版本中,我添加了dataTable