【问题标题】:Datagrid combobox does not bind to a property - wpfDatagrid组合框未绑定到属性-wpf
【发布时间】:2016-12-10 06:44:46
【问题描述】:

我有一个带有 datagridComboBoxColumn 的数据网格。数据网格的项目源是一个名为 Products 的自定义类,它有一个名为 Installer 的属性(也是一个名为 Contact 的自定义类)。

我想将 datagridComboBoxColumn itemsSource 绑定到所有联系人,并将组合框的选定值绑定到安装程序。这不起作用,有人可以帮我吗?提前致谢

不胜感激。我看过其他类似的帖子(例如 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; }
}

【问题讨论】:

  • 也应用 updatesourcetrigger。
  • 对不起@AnjumSKhan,你能说得更详细一点吗?我应该在哪里应用这个?作为 DataGrid 的属性?
  • ... 到您的选定项绑定中的组合框列
  • 谢谢@AnjumSKhan。我做到了(见我的帖子更新)但没有任何改变。在数据网格中,它甚至没有出现组合框。
  • 您的代码中的安装程序在哪里?

标签: wpf combobox datagrid


【解决方案1】:

你在这里做错了几件事。

  1. Contacts 存在于CatalogPage 中,所以{Binding Contacts} 将不起作用。这是因为DataContextDataGridRow 是为该行显示的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. 1234563否则,现在它将工作Combobox -&gt; Installer,反之亦然。

【讨论】:

  • 谢谢!我已经完成了你说的更改,但仍然无法正常工作。有一个改进,现在我可以看到组合框,但它是空的。我想我遗漏了一些明显的东西......我在 Contacts 属性的 get 语句中设置了一个断点,但它并没有在那里中断。当组合框要求联系人时,我期待打破。通常我使用代码隐藏而不是绑定来执行此操作,但是在这里,如果我命名 ComboBox,我无法从代码隐藏访问它,这就是为什么我在这里有点迷路,抱歉。感谢您的帮助@AnjumSKhan
  • @chincheta73 您的“联系人”属性是私有的,请将其设为公开。
  • 我之前已经尝试过了,但抱歉没有更新我的帖子。现在我的帖子更新了,Contacts 属性是公开的,没有任何变化。
  • 您的意思是再次将 DataGridTemplateColumn 更改为 DataGridComboBoxColumn 并将 ItemsSource 设置为您建议的值吗?我已经尝试过了,但仍然无法正常工作。在网格中它甚至没有出现一个组合框。
猜你喜欢
  • 1970-01-01
  • 2018-01-17
  • 2014-10-05
  • 2016-05-02
  • 1970-01-01
  • 2022-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多