2011-01-22 133 views
4

我有一個combobox,似乎沒有更新它的視圖模型。MVVM Combobox綁定

在視圖我有

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

在視圖模型我有

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

組合框被填充好了,我似乎無法得到一個值出來。

回答

14

問題是你正在使用SelectedValuePath =「名稱」只是刪除它,它會工作。

你的ComboBox就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}"/>