从组合框中筛选dataGridComboBox列
本文关键字:dataGridComboBox 筛选 组合 | 更新日期: 2023-09-27 18:19:48
首先我在组合框列中选择一个供应商,然后只有来自该供应商的引用才能填充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>
ViewModel
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; }
}
由于DataGridComboBoxColumn
或任何其他支持的数据网格列不是数据网格可视化树的一部分,因此它们不会继承数据网格的DataContext。因为,它们不在视觉树中,所以任何使用RelativeSource获取DataContext
的尝试都不会奏效。
解决方案-您可以创建一个代理元素来绑定窗口的数据上下文;使用该代理元素来绑定CCD_ 4的CCD_。例如:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication10"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
d:DataContext="{d:DesignInstance local:Vm, IsDesignTimeCreatable=True}">
<Grid>
<Grid.Resources>
<FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<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"/>
<ContentControl Visibility="Collapsed" Content="{StaticResource ProxyElement}"></ContentControl>
<DataGrid x:Name="dataGridInvoice"
ItemsSource="{Binding Collection}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Ref Supplier"
ItemsSource="{Binding DataContext.Reference, Source={StaticResource ProxyElement}}"
DisplayMemberPath="refsup"
SelectedValueBinding="{Binding refSup}"
SelectedValuePath="refsup"
Width="*"/>
</DataGrid.Columns>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>