我有ViewModel两个集合属性。我用来填充DataContext设置为 this的页面上的表单字段的一个集合ViewModel。在这个表格中,在填充了集合的字段中,我还有一个ComboBox我想用其他集合填充的字段。
我使用 Grid 进行布局,并将此 GridDataContext设置为第一个集合。当我有一个ComboBox网格之外时,我可以很容易地用第二个集合填充它。但是一旦我尝试在网格中这样做时,我似乎无法完成它。
在代码片段中,我将 Grid 设置DataContext为ItemTypes. 从ItemTypes字段中可以得到它们的值。虽然ComboBox需要从其他集合中获取其值:
<Grid DataContext="{Binding ItemTypes}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Name:" />
<TextBox Grid.Column="2" Text="{Binding Name}" />
<TextBlock Grid.Row="1" Text="Description:" />
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Description}" />
<TextBlock Grid.Row="2" Text="Manufacturer:" />
<ComboBox Grid.Column="1" Grid.Row="2" />
<TextBlock Grid.Row="3" Text="Short Name:" />
<TextBox Grid.Column="1" Grid.Row="3" Text="{Binding ShortName}" />
<TextBlock Grid.Row="5" Text="Weight:"/>
<TextBox Grid.Column="1" Grid.Row="5" Text="{Binding Weight}" />
</Grid>