MVVM Combobox绑定 [英] MVVM Combobox binding

查看:121
本文介绍了MVVM Combobox绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框似乎没有更新它的视图模型。



在视图上我有

 < ComboBox Grid.Row =0
Grid.Column =1
ToolTip =官员的当前排名
ItemsSource ={Binding Path = RanksAvailable}
DisplayMemberPath =Name
SelectedValuePath =Name
SelectedValue ={Binding Path = SelectedRank,Mode = TwoWay}/>



在视图模型中我有

  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);
}
}
}

正在填充组合框,我只是不能似乎得到一个值出来。

解决方案

问题是你使用SelectedValuePath =Name只是删除它,它会工作。
$ b

您的ComboBox将会成为 -

 < ComboBox Grid.Row =0
Grid.Column =1
ToolTip =官员的当前排名
ItemsSource ={Binding Path = RanksAvailable}
DisplayMemberPath =Name
SelectedValue ={Binding Path = SelectedRank,Mode = TwoWay}/>


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.

解决方案

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

这篇关于MVVM Combobox绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆