无法从组合框中选择值

时间:2018-11-06 11:34:03

标签: c# wpf mvvm binding

我似乎无法从组合框中选择一个值。我查看了其他问题/解决方案,但所有答案或问题似乎都与我的问题无关。

在我看来:

ComboBox Grid.Column="1" ItemsSource="{Binding Path=FileInstructions, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" 
              SelectedItem="{Binding Path=SelectedFileInstruction, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

在我的viewModel中:

    public FileInstructionSelectorControl(Action<FileInstruction> selectionChangedEvent)
    {
        InitializeComponent();
        DataContext = this;
        //_selectionChangedEvent = selectionChangedEvent;
        SetFileInstructions();

        /*var myList = new List<string>() { "Bob" };
        FileInstructions = new ObservableCollection<string>(myList);*/
        SelectedFileInstruction = FileInstructions[0];
    }

    private void SetFileInstructions()
    {
        var instructions = Enum.GetValues(typeof(FileInstruction)).Cast<FileInstruction>();
        FileInstructions = new ObservableCollection<string>(instructions.Select(item => item.ToString()).ToList());
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<string> _fileInstructions;
    public ObservableCollection<string> FileInstructions
    {
        get => _fileInstructions;
        set
        {
            _fileInstructions = value;
            OnPropertyChanged(nameof(FileInstructions));
        }

    }

    private string _selectedFileInstruction;
    public string SelectedFileInstruction
    {
        get => _selectedFileInstruction;
        set
        {
            _selectedFileInstruction = value;
            OnPropertyChanged(nameof(SelectedFileInstruction));
            SelectionChanged();
        }
    }

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void SelectionChanged()
    {
        //_selectionChangedEvent(SelectedFileInstruction);
    }

如您所见,我尝试手动将selectedItem设置为列表中的第一项,并且它可以正确显示 enter image description here

但是我无法从comboBox中选择一个新值。该列表确实存在,但是感觉comboBox已被锁定/禁用,因为单击comboBox时无法显示下拉列表。

编辑:

我可以跳至组合框,并使用键盘更改值,但无法使用鼠标将组合框下拉。

1 个答案:

答案 0 :(得分:0)

此问题与绑定无关,但是根据this question