无法验证我在 WPF DataGridComboBoxColumn 中的选择

发布于 2024-12-14 10:02:50 字数 1416 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-12-21 10:02:50

您没有将 ComboBox 的 SelectedValueSelectedItem 绑定到任何内容,因此不会选择任何项目。

DataGridComboBoxColumn 上设置 SelectedItemBindingSelectedValueBinding 以维护所选项目

You are not binding the SelectedValue or SelectedItem of the ComboBox to anything, so no item will get selected.

Set either the SelectedItemBinding or SelectedValueBinding on your DataGridComboBoxColumn to maintain the selected item

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文