开发者

MVVM Combobox binding

开发者 https://www.devze.com 2023-02-05 21:27 出处:网络
I have a combobox that doesn\'t seem to be updated it\'s view model. On the view I have <ComboBox Grid.Row=\"0\"

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 m开发者_JAVA百科odel 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.


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}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消