如何在DropDownOpened

时间:2017-01-06 09:48:11

标签: c# wpf mvvm combobox eventtrigger

我必须在下拉列表打开时填充下拉框。我的意思是我点击箭头,VM必须填充它然后打开。我有一个ObservableCollection字符串来填充组合框。 MVM也是INotifyPropertyChanged。

<ComboBox x:Name="ServersBox"  Grid.Row="0" Grid.Column="1" Height="23" IsEditable="True" IsSynchronizedWithCurrentItem="True" IsTextSearchEnabled="True" 
        IsTextSearchCaseSensitive="False" StaysOpenOnEdit="True" ItemsSource="{Binding AvailableSqlServer}"  
        SelectedItem="{Binding SelectedSqlServer}" Text="{Binding newServer, UpdateSourceTrigger=LostFocus, Mode=TwoWay}" Width="261">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="DropDownOpened" SourceObject="{Binding ElementName=ServersBox}">
            <i:InvokeCommandAction Command="{Binding OnDropDownOpened}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>


public MigratorSqlViewModel(Migrator _m)
{
    _setdropDownCommand = new RelayCommand<object>(OnDropDownOpened);
}
private RelayCommand<object> _setdropDownCommand;
public RelayCommand<object> SetdropDownCommand
{
    get {return _setdropDownCommand; }
    set { _setdropDownCommand = value; }
}

public void OnDropDownOpened(object obj)
{

    AvailableSqlServer = _Migrator.getServer();

}

public ObservableCollection<string> AvailableSqlServer
{
    set
    {
        this._availableSqlServer = value;
        _Migrator.AvailableSqlServer = _availableSqlServer;
        OnPropertyChanged("AvailableSqlServer");
    }
    get { return _availableSqlServer; }
}

没有发生任何事情。

1 个答案:

答案 0 :(得分:3)

在您的代码中,您命令名为SetdropDownCommand但绑定了OnDropDownOpened。所以请正确绑定命令,如下所示。

<i:InvokeCommandAction Command="{Binding SetdropDownCommand}" />