MVVM Combobox binding

亡梦爱人 提交于 2020-01-02 06:41:21

问题


I have a combobox that doesn't seem to be updated it's view model.

On the view I have

<ComboBox Grid.Row="0" 
           Grid.Column="1" 
           ToolTip="Current rank of the officer" 
           ItemsSource="{Binding Path=RanksAvailable}"
           DisplayMemberPath="Name"
           SelectedValuePath="Name"
           SelectedValue="{Binding Path=SelectedRank, Mode=TwoWay}"/>

in the view model I have

    public List<Rank> RanksAvailable {get; set;}
    private Rank _selectedRank;

    public Rank SelectedRank 
    {
        get { return _selectedRank; }
        set
        {
            if (_selectedRank != value)
            {
                _selectedRank = value;
                this.isDirty = true;
                RaisePropertyChanged("SelectedRank");
            }
        }
    }

the combobox is being populated alright, I just can't seem to get a value out of it.


回答1:


The problem is you are using SelectedValuePath="Name" just remove it and it will work.

Your ComboBox will become-

<ComboBox Grid.Row="0" 
           Grid.Column="1" 
           ToolTip="Current rank of the officer" 
           ItemsSource="{Binding Path=RanksAvailable}"
           DisplayMemberPath="Name"
           SelectedValue="{Binding Path=SelectedRank, Mode=TwoWay}"/>


来源:https://stackoverflow.com/questions/4765362/mvvm-combobox-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!