【问题标题】:How to fill combobox on DropDownOpened如何在 DropDownOpened 上填充组合框
【发布时间】:2017-05-21 01:18:44
【问题描述】:

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

什么都没发生。

【问题讨论】:

    标签: c# wpf mvvm combobox eventtrigger


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。这是工作。你拯救了我的一天。非常感谢)))
    • @Bird75 最欢迎 :-)
    • 如何才能使即使我下拉列表也已加载,但组合框不会选择第一个项目。我的意思是,如果用户不选择某些内容,则所选项目是空的。
    • 在 AvailableSqlServer = _Migrator.getServer() 之后尝试 SelectedSqlServer=AvailableSqlServer.FirstOrDefault();
    • 它选择列表中的第一项并将其设置到组合框中。我认为它一定是 Combobox 属性“selectetItem”中的某个东西。
    猜你喜欢
    • 1970-01-01
    • 2015-06-16
    • 2020-03-14
    • 2015-03-23
    • 1970-01-01
    • 2013-06-22
    • 2011-11-20
    • 2011-12-12
    • 1970-01-01
    相关资源
    最近更新 更多