2017-08-21 40 views
0

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

任何援助將不勝感激。

回答

1

綁定ComboBoxSelectedValue屬性將IntWrapperInt屬性:

<ComboBox DisplayMemberPath="MissionNameAndID" SelectedValuePath="Int" SelectedValue="{Binding Int}" 
      ItemsSource="{Binding MissionListViewModel.MissionVMs, Source={StaticResource Locator}}" /> 

這應該提供的IntWrapper.Int酒店有公共setter和該MissionViewModelInt屬性是相同的請鍵入IntWrapperInt屬性。

0

我試圖根據您的描述爲您的問題創建解決方案。爲了幫助您獲得更多資格,我們需要更多關於MissionViewModel及其中包含的屬性的信息。

我想,你的綁定不指向正確的ViewModel。我嘗試重新創建你在下面描述的內容,在我的情況下,組合框填充了MissionVm類的Name屬性。

這是XAML。在這裏,我將ItemsSource綁定更改爲直接指向父窗口的DataContext的ObservableCollection。

<ListBox x:Name="lbMissionsToGive" ItemsSource="{Binding MissionsToGive}" SelectedItem="{Binding SelectedMissionToGive}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Border BorderThickness="2" BorderBrush="LightBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2" CornerRadius="5,5,5,5"> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding }"></TextBlock> 
         <ComboBox Margin="20" Width="50" ItemsSource="{Binding DataContext.MissionVMs, RelativeSource={RelativeSource AncestorType=Window}}" DisplayMemberPath="Name"></ComboBox> 
        </StackPanel> 
       </Border> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

這是視圖模型根據您的描述:

public class ViewModel : INotifyPropertyChanged 
{ 
    public ViewModel() 
    { 
     var vm1 = new MissionVM { Id = 1, Name = "111"}; 
     var vm2 = new MissionVM { Id = 2, Name = "222" }; 
     var vm3 = new MissionVM { Id = 3, Name = "333" }; 

     MissionVMs = new ObservableCollection<MissionVM> { vm1, vm2, vm3}; 

     MissionsToGive = new ObservableCollection<int> {1, 2, 3}; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    public ObservableCollection<int> MissionsToGive { get; set; } 

    public int SelectedMissionToGive { get; set; } 

    public ObservableCollection<MissionVM> MissionVMs { get; set; } 
} 

public class MissionVM 
{ 
    public int Id { get; set; } 

    public string Name { get; set; } 
} 

這是輸出:

enter image description here

我希望這有助於。如果我誤解了你,請給出進一步的解釋。