2015-04-17 5 views
0

Я пытаюсь получить выбранное значение из списка в команду. Есть ли простой способ привязать commandparameter к выбранному элементу?WPF ComboBox Mvvm binding

<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 привязку и привязать к Пути SelectedItem

+0

Спасибо вот оно работает. – A191919