2017-10-25 4 views
0

私はComboBoxのwpfプロジェクトを持っています。内部のアイテムは動的に埋められます。したがって、Labelとコマンドを含むモデルにバインドされています。WPFのComboBoxItemへのバインドコマンド

ユーザがドロップダウン/ ComboBoxで項目を選択した場合、コマンドを実行する必要があります。私はTextBlockHyperlinkコマンドを含むDataTemplateで試しました。しかしコマンドは、LabelHyperlink)を選択してアイテム全体をクリックした場合にのみ実行されます。

<ComboBox ItemsSource="{Binding Path=States}" SelectedItem="{Binding CurrentState}" > 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock> 
       <Hyperlink Command="{Binding Command}" TextDecorations="None" Foreground="Black"> 
        <TextBlock Text="{Binding Path=Label}"/> 
       </Hyperlink> 
      </TextBlock> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

そこで問題は、私はComboBoxItemに私のコマンドをバインドすることができますどのように、今ありますか?

答えて

1

ComboBoxItemは何Command性質を持っていませんが、あなたはCurrentStateプロパティのセッターからのコマンドを実行することができます:あなたはComboBoxで項目を選択するたび

private State _currentState; 
public State CurrentState 
{ 
    get { return _currentState; } 
    set 
    { 
     _currentState = value; 
     if (_currentState != null) 
     { 
      _currentState.Command.Execute(null); 
     } 
    } 
} 

このプロパティが設定されます。もう1つのオプションは、ビュー内のSelectionChangedイベントを処理することです。

+0

ありがとうございました! –

関連する問題