如何在打开的下拉菜单中填充组合框

How to fill combobox on DropDownOpened

当下拉菜单打开时,我必须填写下拉框。我的意思是我单击箭头,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; }
}

什么都没发生。

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

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