【发布时间】:2015-06-23 15:36:29
【问题描述】:
我正在尝试同时学习 WPF 和 MVVM,并且需要一些有关绑定的帮助。我仍然试图让我的头脑围绕将控件绑定到类的属性。
这是我的故障视图模型:
public class Malfunctions : ViewModelBase {
public ObservableCollection<Model.PartMalfunction> AllPartMalfunctions {
get;
private set;
}
public ObservableCollection<Model.Part> AllParts {
get;
private set;
}
}
这里是 Model.PartMalfunction.cs:
public class PartMalfunction{
public ObservableCollection<Model.PartSerial> AllPartSerials {
get;
set;
}
}
这里是 Model.Part.cs:
public class Part {
public string Label { get; set; }
public string Value { get; set; }
}
我有一个绑定到 Malfunctions ViewModel 中的 AllPartMalfunctions ObservableCollection 的 DataGrid。这个绑定很好用。
我在 RowDetailsTemplate 中嵌套了另一个 DataGrid,它绑定到 PartMalfunction 模型中的 AllPartSerials。这个绑定也很好用。
我的问题在于嵌套 DataGrid 内的组合框。我想将此组合框绑定到 Malfunctions ViewModel 中的 AllParts ObservableCollection。我该怎么做?
<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>
【问题讨论】:
-
包含绑定到 AllPartMalFunctions 的 DataGrid 的元素是什么?
-
@Markus - 如果我理解您的问题,那么您是在问我 XAML 中的控件层次结构,对吗?它先是 DockingPanel,然后是 StackPanel,然后是第一个 DataGrid。
-
我发布的答案应该有效。我有一些类似的代码将绑定路径设置为包含 DataGrid 的元素中的命令。我询问了您的层次结构,以便我了解与故障相关的内容。
标签: wpf data-binding combobox