3

"WPF command support in ComboBox", this page shows how to extend a combobox to support a command, but it didn't give a dome of the delegate command that maps to the combobox's SelectedIndexChanged event. Now the problem I face is how can I handle the combobox SelectedIndexChanged event like where it is a one-off combobox situation :

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

current situation is as below:

<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; }

and the corresponding delegate method :

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

binding method to command:

ChangeStartModeCmd = new DelegateCommand(ChangeStartMode);

I want to define the method like this:

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

but how do I bind it to the delegate command ChangeStartModeCmd?

ChangeStartModeCmd = new DelegateCommand(ChangeStartMode( 
        /*what should I pass for the method?*/));
Community
  • 1
  • 1
夏夜流萤
  • 41
  • 1
  • 9

1 Answers1

3

You probably wont need a CommandSupportedCombobox as you can attach SelectedItem property of you ComboBox and inside the setter in your ViewModel Call the funciton you want...

Xaml

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

ViewModel

 public MyItem
 {
    get {return myItem;}
    set
    {
        myItem=value;
        OnPropertyChanged("MyItem");  implement INotifyPropertyChanged
        MyFavFunction(); // Function to be called
    }
 }
Mark Richman
  • 28,948
  • 25
  • 99
  • 159
Ankesh
  • 4,847
  • 4
  • 38
  • 76