2015-04-17 236 views
0

我正在尝试从列表中选择值到命令。有没有简单的方法将commandparameter绑定到选定的项目?WPF组合框Mvvm绑定

<ComboBox HorizontalAlignment="Left" Margin="-56,10,0,0" VerticalAlignment="Top" Width="120" SelectedIndex="0" ItemsSource="{Binding Time}"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Loaded"> 
       <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding }"></i:InvokeCommandAction> 
      </i:EventTrigger> 
      <i:EventTrigger EventName="SelectionChanged"> 
       <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding }"></i:InvokeCommandAction> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </ComboBox> 

,当我试图像

   <i:EventTrigger EventName="SelectionChanged"> 
       <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}},Path=SelectionBoxItem}"></i:InvokeCommandAction> 
      </i:EventTrigger> 

真奇怪卡住为什么呢?

回答

0
<ComboBox x:Name="Combo" HorizontalAlignment="Left" Margin="-56,10,0,0" VerticalAlignment="Top" Width="120" SelectedIndex="0" ItemsSource="{Binding Time}"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Loaded"> 
       <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding ElementName=Combo, Path=SelectedItem}"></i:InvokeCommandAction> 
      </i:EventTrigger> 
      <i:EventTrigger EventName="SelectionChanged"> 
       <i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="{Binding }"></i:InvokeCommandAction> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </ComboBox> 

如果你给你的组合框的名称,你可以使用ElementName绑定和绑定到Path SelectedItem

+0

感谢的它的工作。 – A191919