2015-02-09 115 views
0

在我的XAML中,DataContext被设置为另一个类(我们称之为ViewModel,用于从接口控件中分离数据)。 我在我的XAML以下组合框:WPF将ComboBox选择绑定到DataContext

<ComboBox Name="cmbList" ItemsSource="{Binding Path=LocationLists, Mode=OneWay}" DisplayMemberPath="Name"></ComboBox> 

因此,LocationLists是我的DataContext类型化的ObservableCollection。

目前,我有以下事件:

Private Sub cmbList_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles cmbList.SelectionChanged 
     Dim ll As LocationList 

     'Get the selected List 
     ll = e.AddedItems(0) 

     'Update the selected ListID in DataContext 
     DataContext.ListID = ll.ID 
End Sub 

,一切工作正常。

我想要什么:如何更新DataContextListID属性总是等于cmbList选择的Location对象的ID财产,没有后面的代码中任何实现?是否有可能只在XAML中使用?因为基本上这只是一个有约束力的问题。

谢谢!

回答

0
<ComboBox ItemsSource="{Binding LocationLists}" 
      DisplayMemberPath="Name" 
      SelectedValue="{Binding ListID}" 
      SelectedValuePath="ID" />