I'm attempting to learn WPF and MVVM at the same time, and need some help with a binding. I'm still trying to get my head wrapped around binding controls to properties of a class.
Here is my Malfunctions ViewModel:
public class Malfunctions : ViewModelBase {
public ObservableCollection<Model.PartMalfunction> AllPartMalfunctions {
get;
private set;
}
public ObservableCollection<Model.Part> AllParts {
get;
private set;
}
}
Here is the Model.PartMalfunction.cs:
public class PartMalfunction{
public ObservableCollection<Model.PartSerial> AllPartSerials {
get;
set;
}
}
Here is the Model.Part.cs:
public class Part {
public string Label { get; set; }
public string Value { get; set; }
}
I have a DataGrid that binds to the AllPartMalfunctions ObservableCollection in the Malfunctions ViewModel. This binding is working just great.
I have another DataGrid nested in the RowDetailsTemplate, and it is binding to AllPartSerials in the PartMalfunction model. This binding is working just fine as well.
My problem is with a combobox that is inside of the nested DataGrid. I want to bind this combobox to the AllParts ObservableCollection in the Malfunctions ViewModel. How do I do this?
<DataGrid ItemsSource="{Binding AllPartMalfunctions}" AutoGenerateColumns="False" Width="Auto"
RowDetailsVisibilityMode="Visible">
<DataGrid.Columns>
<!--removed for brevity-->
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding AllPartSerials }" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="cboPart" VerticalAlignment="Center" ItemsSource="{Binding AllParts}" DisplayMemberPath="Label" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>