好吧,所以我有,如我所说,列表框中的组合框。这些都是控件(在Visual Studio 2010和MVVM中使用C#的WPF项目)。列表框中的组合框和相关的数据绑定问题
我可以创造他们一切都很好,很好。
ListBox包含一个名为IntWrapper的ObservableCollection,它只包含和int。
ComboBox加载了一个名为MissionViewModel的ObservableCollection,它只是一个包含几个基本数据类型的类。
因此,ListBox和ComboBox的ItemSource是不同的。
我可以通过一个按钮添加项目到ListBox,它只是向ObservableCollection添加一个新的IntWrapper。在视觉上,这给了我一个新的ComboBox,它被填充。
我无法弄清楚的是,当我选择它时,如何获取MissionViewModel的某个属性以转到IntWrapper中的属性。
以前我只是用ListBox中的一个文本框,它是这样的:
<ListBox x:Name="lbMissionsToGive" ItemsSource="{Binding MissionsToGive}" SelectedItem="{Binding SelectedMissionToGive}" ContextMenu="{StaticResource RemoveMissionToGiveMenu}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="2" BorderBrush="LightBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2" CornerRadius="5,5,5,5">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="1">Mission ID: </TextBlock>
<TextBox Margin="1" Text="{Binding Int, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的(非工作)的尝试,由此我对ComboBox代替文本框是这样的:
<ListBox x:Name="lbMissionsToGive" ItemsSource="{Binding MissionsToGive}" SelectedItem="{Binding SelectedMissionToGive}" ContextMenu="{StaticResource RemoveMissionToGiveMenu}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="2" BorderBrush="LightBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2" CornerRadius="5,5,5,5">
<StackPanel Orientation="Horizontal">
<ComboBox DisplayMemberPath="MissionNameAndID" SelectedValuePath="Int" ItemsSource="{Binding MissionListViewModel.MissionVMs, Source={StaticResource Locator}}"></ComboBox>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我曾经以为SelectedValuePath将是我想要的东西,但目前为止这似乎不起作用(也许我错误地使用它)。
任何援助将不胜感激。