2012-07-23 19 views
5

私は、選択した値が変更されるたびに別々のコマンドをトリガーする単純なComboBoxを持っています。WPFのComboBoxItemへのコマンドのバインド

<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleViewFeedViewManual}"> 
    <ComboBox Margin="3,3,0,0"> 
     <ComboBoxItem IsEnabled="{Binding CanSelectViewFeedData}" > 
      <ComboBoxItem.CommandBindings> 
       <CommandBinding Command="SelectViewFeedDataCommand" /> 
      </ComboBoxItem.CommandBindings> 
      <TextBlock Text="View Feed Data"/> 
     </ComboBoxItem> 
     <ComboBoxItem IsEnabled="{Binding CanSelectViewManualData}"> 
      <ComboBoxItem.CommandBindings> 
       <CommandBinding Command="SelectManualFeedDataCommand" /> 
      </ComboBoxItem.CommandBindings> 
      <TextBlock Text="View Manual Data"/> 
     </ComboBoxItem> 
    </ComboBox> 
</WrapPanel> 

私は「は 『SelectViewFeedDataCommand』を変換できません」というエラーが出ます:ここに私のマークアップの例があります。他のComboBoxItemについても同様のエラーが発生します。 ICommandは、UserControlのDataSourceであるViewModelクラスで定義され、DataTemplateとしてバインドされています。

public ICommand SelectViewFeedDataCommand 
{ 
    get 
    { 
     // Code to perform 
    } 
} 

私はかなり広範囲にこれを研究してきたが、効果的にComboBoxItemへのICommandをバインドする方法については答えを見つけることができます。

これは、ラジオボタンと関連コマンドのセットを使用していた既存のコードからこれを適用しています。これは非常に簡単です。 ComboBoxでこれを行う簡単な方法はありませんか?

ありがとうございました。

+1

「ComboBox.SelectedItem」プロパティを何かにバインドして、SelectedComboBoxItemプロパティの「PropertyChanged」イベントでコマンドロジックを実行するとどうなりますか? – Rachel

答えて

0

私は私の古い記事を見ていました私の質問に解決策を掲載したことがないことに気付きました。

<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleFeedViewManual}" > 
    <ComboBox Margin="10,10,10,10" Width="200" ItemsSource="{Binding AvailableDataSources}" SelectedValue="{Binding SelectedDataSource}"/> 
</WrapPanel> 

とビューモデルから:私はちょうどそのSelectedValueのが変更されたとき、私のコンボボックス「選択項目」を含んでいて、私のViewModelにリストを作成し、プロパティのセッターで私の「変更」のロジックをトリガー:

public FeedSource SelectedDataSource 
    { 
     get { return _selectedDataSource; } 
     set 
     { 
      _selectedDataSource = value; 
      base.OnPropertyChanged("SelectedDataSource"); 

      //additional code to perform here 

     } 
    } 
0

バインドにコマンドを入れてみましたか?更新戦略の

<CommandBinding Command="{Binding SelectManualFeedDataCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" /> 

編集:

ComboBoxアイテムが結合ダイレクトコマンドをサポートしていませんので、添付プロパティを作成してみてください:

Link

+0

はい、あります。 "Binding 'は、' CommandBinding 'タイプの' Command 'プロパティで設定することはできません。' Binding 'はDependencyObjectのDependencyPropertyでのみ設定できます。" – jpaull