3

WPF command support in ComboBox ”,此页面展示了如何扩展组合框以支持命令,但它没有给出映射到组合框的 SelectedIndexChanged 事件的委托命令的圆顶。现在我面临的问题是如何处理组合框 SelectedIndexChanged 事件,就像它是一次性组合框情况一样:

<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

您可能不需要,CommandSupportedCombobox因为您可以附加SelectedItemComboBox 的属性并在setterViewModel 中调用您想要的功能......

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 回答