2016-08-04 74 views
0

我有一个datagrid与datagridComboBoxColumn。 datagrid的项目源是一个名为Products的自定义类,它具有一个名为Installer的属性(也称为Contact的自定义类)。Datagrid组合框不绑定到属性 - wpf

我想要将datagridComboBoxColumn项目源绑定到所有联系人,并将选定的comboBox值赋值给安装程序。这不起作用,有谁能请给我一个手吗?在此先感谢

这将不胜感激。我看过其他类似的帖子(如this onethis one),但情况并不完全相同。

我的XAML代码:

<DataGrid x:Name="productsList" AutoGenerateColumns="False" IsReadOnly="True" CanUserResizeRows="False" 
       CanUserResizeColumns="True" ColumnWidth="*" GridLinesVisibility="None"> 
       <DataGrid.Columns> 

        <DataGridTextColumn Header="Ref" 
            Binding="{Binding Ref}" 
            /> 
        <DataGridTextColumn Header="Product" 
            Binding="{Binding Product}" 
            /> 
        <DataGridComboBoxColumn Header="Installer" SelectedItemBinding="{Binding Installer, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Contacts}"/> 

       </DataGrid.Columns> 
</DataGrid> 

我的后台代码:

public partial class CatalogPage : Page 
{ 
    ObservableCollection<CatalogProduct> mProductList = new ObservableCollection<CatalogProduct>(); 

    public ObservableCollection<Contact> Contacts 
    { 
     get 
     { 
      return Parent.mContactsPage.GetContacts(); 
     } 
    } 

    private LocalConfigurationPage Parent { get; set; } 
    public CatalogPage(LocalConfigurationPage localConfigurationPage) 
    { 
     InitializeComponent(); 

     Parent = localConfigurationPage; 

     productsList.ItemsSource = mProductList; 


    } 
} 

这是CatalogProduct类:

public class CatalogProduct 
{ 
    public string Ref { get; set; } 
    public string Product { get; set; } 
    public Contact Installer { get; set; } 
} 
+0

也适用更新来源触发器。 – AnjumSKhan

+0

对不起@AnjumSKhan,你能指定一些更详细的吗?我应该在哪里应用?作为DataGrid的属性? – chincheta73

+0

...在您的selecteditem绑定中的comboboxcolumn – AnjumSKhan

回答

0

夫妇的你做错了这里的东西。

  1. Contacts存在于CatalogPage所以,{Binding Contacts}不会工作。这是因为DataGridRowDataContext是该行所示的Item。对于你的行,它将是CatalogProduct,并且在那里没有Contacts

    相反,你必须这样做:

    ItemsSource="{Binding DataContext.Contacts, RelativeSource={RelativeSource AncestorType=DataGrid}}

  2. 其次,有已知问题DataGridComboBoxColumn,所以总是用这样的:

    <DataGridTemplateColumn> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <ComboBox SelectedItem="{Binding Installer, UpdateSourceTrigger=PropertyChanged}}" ItemsSource="{Binding DataContext.Contacts, RelativeSource={RelativeSource AncestorType=DataGrid}}"/> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
    
  3. 最后,如果你想用Installer值更新ComboBoxColumn,为Installer实施更改通知,并为设置。否则,现在它将工作Combobox -> Installer而不是反之亦然。

+0

谢谢!我已经完成了你所说的改变,但仍然没有奏效。有一个改进,现在我可以看到组合框,但它是空的。我想我错过了一些明显的... 我已经在Contacts属性的get语句中设置了一个断点,但它并没有在那里突破。当组合框要求联系人时,我期待中断。 通常我使用代码隐藏而不是绑定来实现,但在这里,如果我命名组合框,我无法从代码隐藏中访问它,这就是为什么我在这里有点失落,对不起。感谢您的帮助@AnjumSKhan – chincheta73

+0

@ chincheta73您的联系人属性是私人的,使其公开。 – AnjumSKhan

+0

我已经尝试过,但没有更新我的帖子对不起。现在我的帖子已更新,联系人属性是公开的,没有任何更改。 – chincheta73