2014-07-24 139 views
0

你好我尝试从绑定对象列表中检索一个对象。我使用MVVM风格 所以我有一个类名Channel。通道具有以下属性:字符串名称,字符串标签,诠释标识,诠释assignedLocation等。从绑定属性检索对象wpf

我也有一个名为ChannelSetup.xaml XAML文件,其中有我的ChannelSetup是

<ListView x:Name="ListView"> 
    <ListView.View> 
    <GridView> 
     <GridViewColumn DisplayMemberBinding="{Binding Name}"/> 
     <GridViewColumn Width="65" Header="Channel #"> 
     <GridViewColumn.CellTemplate> 
      <DataTemplate> 
      <ComboBox "Loaded="LineComboBox_OnLoaded".... 
     </GridViewColumn> 

.xaml.cs文件我有这样的事情

this.AvailableChannelLines = new ObservableCollection<Channel>(); 
this.DataContext = this; 
this.ListView.ItemsSource = this.AvailableChannelLines; 

它正确填充我的列表视图,一切都很好..

private void LineComboBox_OnLoaded(object sender, RoutedEventArgs e) 
{ 
//// HERE I NEED TO GET THE CURRENT CHANNEL OBJECT 
} 

但是当LineComboBox_OnLoaded事件被调用时,我希望能够知道/获取当前绑定到的通道对象。如何做到这一点或我需要使用不同的方法,方法或事件?

回答

0

DataContext comboBox将指向当前频道。

private void LineComboBox_OnLoaded(object sender, RoutedEventArgs e) 
{ 
    Channel currentChannel = (sender as ComboBox).DataContext as Channel; 
} 
+1

感谢快,我可以发誓我已经尝试过,但它没有为我工作,但现在它确实感谢了很多。 – kahizer