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

标签 c# wpf mvvm combobox eventtrigger

当下拉菜单打开时,我必须填充下拉框。我的意思是我单击箭头,虚拟机必须填充它然后打开。我有一个 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,但您绑定(bind)了OnDropDownOpened。所以请正确绑定(bind)命令如下。

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

关于c# - 如何在打开的下拉菜单中填充组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41502964/

相关文章:

c# - Wpf - 如何在特定单元格编辑结束后以编程方式结束行编辑

c# - Wpf中的中继命令

c# - 使用 ExecuteSqlCommand 调用存储过程(需要未提供的参数)

c# - 我的算法中找到每对的总和不整除 K 的最大子集的缺陷在哪里?

c# - 为什么使用命名空间以一种不直观的方式工作?

c# - 如何将 XAML 代码的动画转换为 C#

wpf - 从 ViewModel 中设置的属性中取消用户编辑?

C# Service Application 启动无限线程

wpf - 包含表面交互元素的 ScatterViewItems

c# - 禁用功能区的访问键功能(助记符)