Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle SelectionChanged event in via delegate command when the combobox is in a ListView cell

"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?*/));
like image 582
夏夜流萤 Avatar asked Dec 05 '25 02:12

夏夜流萤


1 Answers

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
    }
 }
like image 85
Ankesh Avatar answered Dec 06 '25 15:12

Ankesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!