2017-08-21 39 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

我希望这有助于。如果我误解了你,请给出进一步的解释。