在我的WPF MVVM项目中,我有一个DataGrid,其中包含一个ObservableCollection(CaseCardEquipmentList)和一个ComboBox列。 ComboBox列中的每个ComboBox都需要填充相对于其各自行中的值的ObservableCollection(SerialNumList)。这可能吗?例如,如果DataGrid中有以下值:
Equip Type | OEM Name | Equip Name | Equip Model | Serial Num |
-----------|----------|------------|-------------|------------|
Nav | Global | System 2 | v.2.1 | COMBOBOX |
Shoes | Footwear | Adidas | Old | COMBOBOX |
Shirt | UA | Clothing | Smelly | COMBOBOX |
... ComboBoxes需要显示与底层识别属性相关的所有Serial Nums(SerialNumList)" OPEquipIDNum"在ObservableCollection CaseCardEquipmentList的每一行中。我该怎么做?
XAML
<Grid Grid.Row="2">
<DataGrid HorizontalAlignment="Center"
Width="Auto"
ItemsSource="{Binding CaseCardEquipmentList}"
AutoGenerateColumns="False"
CanUserAddRows="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Equipment Type" Width="Auto" Binding="{Binding EquipTypeName}"/>
<DataGridTextColumn Header="OEM Name" Width="Auto" Binding="{Binding OEMName}"/>
<DataGridTextColumn Header="Equipment Name" Width="Auto" Binding="{Binding EquipName}"/>
<DataGridTextColumn Header="Equipment Model" Width="Auto" Binding="{Binding EquipModel}"/>
<DataGridTemplateColumn Header="SerialNum">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding SerialNumList}"
DisplayMemberPath="SerialNum"
SelectedValuePath="FacilityEquipIDNum"
SelectedValue="{Binding SelectedFacEquipIDNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
视图模型
private ObservableCollection<ALLCaseCardEquipPrep> _caseCardEquipmentList;
public ObservableCollection<ALLCaseCardEquipPrep> CaseCardEquipmentList
{
get { return _caseCardEquipmentList; }
set
{
SetProperty(ref _caseCardEquipmentList, value, () => CaseCardEquipmentList);
if (_caseCardEquipmentList != null)
{
GetSerialNumList(CaseCardEquipmentList);
}
}
}
public ObservableCollection<FacilityEquipment> SerialNumList
{
get { return _serialNumList; }
set { SetProperty(ref _serialNumList, value, () => SerialNumList); }
}
public Int32 SelectedFacEquipIDNum
{
get { return _selectedFacEquipIDNum; }
set { SetProperty(ref _selectedFacEquipIDNum, value, () => SelectedFacEquipIDNum); }
}
public void GetSerialNumList(ObservableCollection<ALLCaseCardEquipPrep> ccel)
{
foreach (ALLCaseCardEquipPrep item in ccel.ToList())
{
facilityequipment = new FacilityEquipment();
facilityequipment.fillFacEquipSerialNumList(item.OPEquipIDNum);
SerialNumList = facilityequipment.FacilityEquipmentList;
}
}
MODEL
public FacilityEquipment(int count, int facilityequipmentidnum, string serialnum)
{
Count = count;
FacilityEquipmentIDNum = facilityequipmentidnum;
SerialNum = serialnum;
FacilityEquipmentList = new ObservableCollection<FacilityEquipment>();
}
public void fillFacEquipSerialNumList(int opequipidnum)
{
using (MySqlConnection con = new MySqlConnection(dbConnectionString))
{
FacilityEquipmentList = new ObservableCollection<FacilityEquipment>();
con.Open();
string Query = "SELECT Fac_Equip_ID_Num, Serial_Num FROM facility_equipment WHERE OP_Equip_ID_Num ='" + opequipidnum + "' ";
MySqlCommand createCommand = new MySqlCommand(Query, con);
MySqlDataReader dr = createCommand.ExecuteReader();
int count = 1;
while (dr.Read())
{
int FacilityEquipmentIDNum = dr.GetInt32(0);
string SerialNum = dr.GetString(1);
FacilityEquipment facequip = new FacilityEquipment(count, FacilityEquipmentIDNum, SerialNum);
FacilityEquipmentList.Add(facequip);
count++;
}
con.Close();
}
}
答案 0 :(得分:0)
试试这个:
1.在ViewModel中选择一个名为“AllSerialNumbers”的ObservalbleCollection / List(我在下面的例子中使用过)。
2.获取该产品所选序列号的ID /唯一。 (例如 - SelectedSerialNoId)
3.查看以下绑定示例。这段代码将找到当前窗口的DataContext,它将从ObeservableCollection中找到所有的serailnumbers(步骤1)
<DataGridComboBoxColumn Header="SerialNumber"
SelectedValueBinding="{Binding SelectedSerialNoId}"
SelectedValuePath="SerialNoId"
DisplayMemberPath="SerialNumber"
>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.AllSerialNumbers}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>