无法验证我在 WPF DataGridComboBoxColumn 中的选择
我有一个 WPF DataGrid,其中一列(实际上是 DataGridComboBoxColumn)绑定到一个列表,绑定工作得很好。问题是,当我从 ComboBox 的列表中选择一个元素时,它会填充该字段,但一旦我离开它,它就会变成空白,这是我的代码:
XAML:
<DataGrid Name="dgPrdCmd" AutoGenerateColumns="False" CanUserAddRows="True"CanUserDeleteRows="True">
<DataGrid.ItemsSource>
<collections:ArrayList>
<local:TestData></local:TestData>
</collections:ArrayList>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Product Name" x:Name="cb_NomPrdCmd" IsReadOnly="False"/>
<DataGridTextColumn Header="Quantity"/>
<DataGridTextColumn Header="Unit Price"/>
</DataGrid.Columns>
</DataGrid>
这是我的 C#:
using (_entieties)
{
IQueryable<string> pName = from Product in _entieties.Products
select Product.prdName;
foreach (var name in pName)
{
prd.Add(id); //prd is just a list of strings
}
}
//here is the most important part
cb_NomPrdCmd.ItemsSource = prd;
提前thanx
I have a WPF DataGrid where a column, actually a DataGridComboBoxColumn, is bound to a List, the binding works just fine. The problem is when I choose an element from the ComboBox's list it fills the field, but it becomes blank once I leave it, here is my code :
XAML :
<DataGrid Name="dgPrdCmd" AutoGenerateColumns="False" CanUserAddRows="True"CanUserDeleteRows="True">
<DataGrid.ItemsSource>
<collections:ArrayList>
<local:TestData></local:TestData>
</collections:ArrayList>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Product Name" x:Name="cb_NomPrdCmd" IsReadOnly="False"/>
<DataGridTextColumn Header="Quantity"/>
<DataGridTextColumn Header="Unit Price"/>
</DataGrid.Columns>
</DataGrid>
and here's my C# :
using (_entieties)
{
IQueryable<string> pName = from Product in _entieties.Products
select Product.prdName;
foreach (var name in pName)
{
prd.Add(id); //prd is just a list of strings
}
}
//here is the most important part
cb_NomPrdCmd.ItemsSource = prd;
thanx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将 ComboBox 的
SelectedValue
或SelectedItem
绑定到任何内容,因此不会选择任何项目。在
DataGridComboBoxColumn
上设置SelectedItemBinding
或SelectedValueBinding
以维护所选项目You are not binding the
SelectedValue
orSelectedItem
of the ComboBox to anything, so no item will get selected.Set either the
SelectedItemBinding
orSelectedValueBinding
on yourDataGridComboBoxColumn
to maintain the selected item