【问题标题】:Cant select value from combobox无法从组合框中选择值
【发布时间】:2018-11-06 21:32:36
【问题描述】:

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

在我看来:

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

在我的视图模型中:

    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 设置为列表中的第一项,并且显示正确

但我无法从组合框中选择新值。该列表确实填充,但感觉组合框被锁定/禁用,因为单击组合框时我无法获得下拉菜单。

编辑:

我可以使用 Tab 切换到组合框,并使用键盘更改值,但无法使用鼠标使组合框下拉。

【问题讨论】:

  • 您所谓的“viewModel”实际上是您的 FileInstructionSelectorControl 背后的代码,对吧?需要注意的是,似乎没有必要使用 ObservableCollection 作为 FileInstructions 属性的类型,因为您似乎从来没有在集合中添加或删除元素。也不清楚为什么你有这种控制权。 XAML 中除了 ComboBox 之外还有什么?它应该如何使用?
  • 正确,它不是 - 真正的 - 视图模型,而是代码隐藏。是的,我知道我可以使用 IEnum。或 List,ObsColl 只是尝试尝试使其正常工作
  • 另外注意,在两个绑定上设置Mode=TwoWayUpdateSourceTrigger=PropertyChanged 是多余的。它对 ItemSource 绑定没有影响,因为 ItemsControl 永远不会更新该绑定的源。 SelectedItem 属性默认为双向绑定,并且在其值更改时已更新源。
  • 您可能希望从控件构造函数中删除 DataContext = this 并将 RelativeSource={RelativeSource AncestorType=UserControl} 添加到 ComboBox 属性绑定。
  • 当我说“一定有一些东西你没有向我们展示”时,这就是我的意思。所有其他注释都不是为了解决您的问题,而只是备注。

标签: c# wpf mvvm binding


【解决方案1】:

此问题与绑定无关,但似乎与硬件有关,根据 this question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多