3

ComboBox での WPF コマンドのサポート」、このページでは、コマンドをサポートするためにコンボボックスを拡張する方法を示していますが、コンボボックスの SelectedIndexChanged イベントにマップされるデリゲート コマンドのドームは提供されませんでした。今私が直面している問題は、コンボボックスの SelectedIndexChanged イベントを、1 回限りのコンボボックスの状況のように処理する方法です。

<ComboBox SelectionChanged="ComboBox_SelectionChanged"></ComboBox>
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var combobox = sender as ComboBox;
    if (combobox.SelectedIndex == 0)
    { 
        //todo:
    }
}

現在の状況は以下の通りです。

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <Ext:CommandSupportedComboBox SelectedIndex="{Binding StartMode}" 
              Command="{Binding ChangeStartModeCmd}">
            <ComboBoxItem>Automatically</ComboBoxItem>
            <ComboBoxItem>Manual</ComboBoxItem>
            <ComboBoxItem>Forbidden</ComboBoxItem>
        </Ext:CommandSupportedComboBox>
    </DataTemplate>
</GridViewColumn.CellTemplate>

/// <summary>
/// change service start mode command
/// </summary>
public ICommand ChangeStartModeCmd { get; private set; }

および対応するデリゲート メソッド:

/// <summary>
/// change service start mode
/// </summary>
public void ChangeStartMode()
{ 
    //todo:
}

コマンドへのメソッドのバインド:

ChangeStartModeCmd = new DelegateCommand(ChangeStartMode);

次のようにメソッドを定義したいと思います。

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var combobox = sender as ComboBox;
    if (combobox.SelectedIndex == 0)
    { 
        //todo:
    }
}

しかし、デリゲート コマンド ChangeStartModeCmd にバインドするにはどうすればよいでしょうか。

ChangeStartModeCmd = new DelegateCommand(ChangeStartMode( 
        /*what should I pass for the method?*/));
4

1 に答える 1

3

ComboBoxのプロパティをCommandSupportedComboboxアタッチし、ViewModel内で必要な機能を呼び出すことができるので、おそらく必要はありません...SelectedItemsetter

Xaml

<ComboBox SelectedItem="{Binding MyItem,Mode=TwoWay}" />

ビューモデル

 public MyItem
 {
    get {return myItem;}
    set
    {
        myItem=value;
        OnPropertyChanged("MyItem");  implement INotifyPropertyChanged
        MyFavFunction(); // Function to be called
    }
 }
于 2012-04-23T04:39:44.263 に答える