刷新 ComboBox with DropDown Opened 导致丢失选择

Refresh ComboBox with DropDownOpened result in loosing selected

我有一个包含活动网络接口条目的组合框。我喜欢在用户每次打开 DropDown 时刷新条目。

遗憾的是,如果 ComboBox 打开然后关闭(箭头在右侧),所选条目将不再显示。 ComboBox 显示为空。 My ComboBox before and after

如果我删除 DropDownOpened 事件,则在打开和关闭 DropDown 后,所选项目将显示在 ComboBox 中。

感谢您的帮助。

这是我的观点:

                    <ComboBox ItemsSource="{Binding HostInterfaces}"
                              SelectedItem="{Binding HostInterfaceSelected, Mode=TwoWay}"
                              DisplayMemberPath="Indicator">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="DropDownOpened">
                                <i:InvokeCommandAction Command="{Binding Path=DropDownOpenedCommand, Mode=OneWay}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </ComboBox>

这是我的视图模型:

    private RelayCommand _dropDownOpenedCommand;
    public ICommand DropDownOpenedCommand => _dropDownOpenedCommand ??
                                             (_dropDownOpenedCommand =
                                                 new RelayCommand(ExecuteDropDownOpenedCommand));

    private void ExecuteDropDownOpenedCommand(object obj)
    {
        HostInterfaces.Clear();

        // add several entries
        _hostInterfaces.Add( ... );
    }

    private readonly ObservableCollection<HostNetworkInterfaceEntry> _hostInterfaces = new ObservableCollection<HostNetworkInterfaceEntry>();
    public ObservableCollection<HostNetworkInterfaceEntry> HostInterfaces => _hostInterfaces;
    private HostNetworkInterfaceEntry _hostInterfaceSelected;
    public HostNetworkInterfaceEntry HostInterfaceSelected
    {
        get => _hostInterfaceSelected;
        set
        {
            if (value == null) return;
            if (value == _hostInterfaceSelected) return;
            _hostInterfaceSelected = value;
            OnPropertyChanged(nameof(HostInterfaceSelected));
        }
    }

我的 HostNetWorkInterfaceEntry class:

public class HostNetworkInterfaceEntry
{
    public HostNetworkInterfaceEntry(string name, IPAddress address)
    {
        Name = name;
        Address = address;
    }

    public string Name { get; private set; }
    public IPAddress Address { get; private set; }
    public string Indicator => (Address != null ? $"{Address}   {Name}" : $"{Name}");
}

感谢@Senir 和@Jeroen van Langen。你的提示引导我找到解决方案。

我不得不修改 属性 HostInterfaceSelected 和 ExecuteDropDownOpenedCommand 方法。

public HostNetworkInterfaceEntry HostInterfaceSelected
{
    get => _hostInterfaceSelected;
    set
    {
        // removed that -> if (value == null) return;
        if (value == _hostInterfaceSelected) return;
        _hostInterfaceSelected = value;
        OnPropertyChanged(nameof(HostInterfaceSelected));
    }
}

private void ExecuteDropDownOpenedCommand(object obj)
{
    var saveSelected = HostInterfaceSelected; // added this
    HostInterfaces.Clear();

    // add several entries
    _hostInterfaces.Add( ... );

    // restore saved to HostInterfaceSelected 
    if (saveSelected != null)
        HostInterfaceSelected = _hostInterfaces.FirstOrDefault(s => s.Name == saveSelected.Name && s.Address != null && s.Address.Equals(saveSelected.Address));
}