从ComboBox过滤dataGridComboBox列

时间:2015-10-30 02:22:29

标签: c# wpf datagridcomboboxcolumn

首先,我在ComboBox列中选择供应商,然后只有来自此供应商的引用才能填充dataGridComboBox列。

我以为我知道该怎么做,但显然缺少了一些东西。我没有任何错误,但我的dataGridComboBox列仍然是空的。

查看

<ComboBox x:Name="supplierComboBox"
                      Margin="5" 
                      ItemsSource="{Binding Collection, Source={StaticResource supplier}}" 
                      DisplayMemberPath="supplier"
                      SelectedItem="{Binding Supplier,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                      SelectedValuePath="idfoodSupplier"
                      Text="{Binding SupplierText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                      Width="160"/>
<DataGrid x:Name="dataGridInvoice" 
                      ItemsSource="{Binding Collection}" 
                      AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridComboBoxColumn Header="Ref Supplier"
                                            ItemsSource="{Binding Reference, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                            DisplayMemberPath="refsup" 
                                            SelectedValueBinding="{Binding refSup}" 
                                            SelectedValuePath="refsup"
                                            Width="*"/>  
                </DataGrid.Columns>
            </DataGrid>

视图模型

public class InvoiceViewModel : ViewModelBase
    {


        public Context ctx = new Context();

        Invoice invoice;

        public InvoiceViewModel()
        {
            Collection = new ObservableCollection<PreInvoice>();
        }

        private ObservableCollection<PreInvoice> collection;

        public ObservableCollection<PreInvoice> Collection
        {
            get
            {
                return collection;
            }
            set
            {
                collection = value;
                OnPropertyChanged("Collection");
            }
        }

        private foodSupplier _supplier;
        public foodSupplier Supplier
        {
            get
            {
                return _supplier;
            }
            set
            {
                _supplier = value;
                OnPropertyChanged("Supplier");
                Reference = new List<product>();
                Reference = (from p in ctx.products
                              where p.supplier == _supplier.idfoodSupplier
                              select p).ToList();
            }
        }

        private List<product> _reference;

        public List<product> Reference
        {
            get
            {
                return _reference;
            }
            set
            {
                _reference = value;
                OnPropertyChanged("Reference");
            }
        }


        public class PreInvoice : ViewModelBase
        {
            public string refSup {get; set;}


        }
    }

产品型号

public partial class product
{
    public int idproduct { get; set; }
    public Nullable<int> supplier { get; set; }
    public string refsup { get; set; }
    public string description { get; set; }

    public virtual foodSupplier foodSupplier { get; set; }

}

1 个答案:

答案 0 :(得分:1)

由于DataGridComboBoxColumn或任何其他受支持的数据网格列不属于datagrid的可视树,因此它们不会继承datagrid的DataContext。因为,它们不在于可视化树,所以任何尝试使用RelativeSource获取DataContext都不会起作用。

解决方案 - 您可以创建一个代理元素来绑定窗口的数据上下文;使用该代理元素绑定ItemsSource的{​​{1}}。例如:

DataGridComboBoxColumn
相关问题