好吧,所以我有,如我所說,列表框中的組合框。這些都是控件(在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將是我想要的東西,但目前爲止這似乎不起作用(也許我錯誤地使用它)。
任何援助將不勝感激。