How to fill combobox on DropDownOpened

1.7k views Asked by At

i have to fill the dropdown box when dropdown is open. I mean i click on the arrow and the VM have to fill it an then Open. I have a ObservableCollection of strings to fill in the combobox. The MVM is also 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; }
}

It happens nothing.

1

There are 1 answers

10
Vijay On BEST ANSWER

In your code you command name SetdropDownCommand but you bound OnDropDownOpened. So please bind the command correctly as follows.

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