2015-04-17 277 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