2011-07-15 43 views
1

在這個區域有不少文章,但沒有人幫助我...這裏是場景:我有兩個「季節」下拉菜單來模擬範圍。如果你在開始範圍內選擇一個季節,viewmodele自動將綁定到結束範圍的屬性設置爲同一個季節(所以它默認爲一年而不是範圍。這是XAML的外觀(刪除了很​​多爲便於閱讀格式attibutes):WPF MVVM Light - SelectedItem的綁定不會改變

<ComboBox ItemsSource="{Binding AvailableSeasons, Mode=OneWay}" 
      SelectedItem="{Binding SelectedBeginRangeSeason, Mode=TwoWay}" 
      ItemTemplate="{DynamicResource SeasonItemShortFormat}" /> 
<ComboBox ItemsSource="{Binding AvailableSeasons, Mode=OneWay}" 
      SelectedItem="{Binding SelectedEndRangeSeason, Mode=TwoWay}" 
      ItemTemplate="{DynamicResource SeasonItemShortFormat}" /> 

在視圖模型的屬性是這樣的:

private Season _selectedBeginRangeSeason; 
private const string SelectedBeginRangeSeasonPropertyName = "SelectedBeginRangeSeason"; 
public Season SelectedBeginRangeSeason { 
    get { return _selectedBeginRangeSeason; } 
    set { 
    if (_selectedBeginRangeSeason != value) { 
     var oldValue = _selectedBeginRangeSeason; 
     _selectedBeginRangeSeason = value; 

     RaisePropertyChanged<Season>(SelectedBeginRangeSeasonPropertyName, oldValue, value, true); 
    } 
    } 
} 

private Season _selectedEndRangeSeason; 
private const string SelectedEndRangeSeasonPropertyName = "SelectedEndRangeSeason"; 
public Season SelectedEndRangeSeason { 
    get { return _selectedEndRangeSeason; } 
    set { 
    if (_selectedEndRangeSeason != value) { 
     Debug.WriteLine("Updating property SelectedEndRangeSeason..."); 

     var oldValue = _selectedEndRangeSeason; 
     _selectedEndRangeSeason = value; 

     Debug.WriteLine("Broadcasting PropertyChanged event for property SelectedEndRangeSeason..."); 
     RaisePropertyChanged<Season>(SelectedEndRangeSeasonPropertyName, oldValue, value, true); 
    } 
    } 
} 

private void UpdateSelectedSeasonSelectors() { 
    // if the end range isn't selected... 
    if (_selectedEndRangeSeason == null) { 
    // automatically select the begin for the end range 
    SelectedEndRangeSeason = _selectedBeginRangeSeason; 
    } 
} 

我已經驗證了高端物業正在與調試語句和單元測試都修改了,但是當我選擇它時,UI並沒有改變......無法弄清楚發生了什麼,並且看到了很多不同的方式......

+0

[This question](http://stackoverflow.com/questions/1605939/wpf-combobox-selecteditem-not-updating)聽起來和你的相似。你有沒有嘗試過那個答案? – RMart

回答

1

嘗試從ViewModel觸發事件以通知UI刷新日曆。

2

您是否收到AvailableSeasons集合中的SelectedSeason?如果沒有,你是否實施了特別的比較四季?

例如,假設你有

<ComboBox ItemsSource="{Binding AvailableSeasons}" 
      SelectedItem="{Binding SelectedSeason}" /> 

如果SelectedSeason = new Season();的的SelectedItem綁定,因爲new Season();不存在AvailableSeasons將無法​​正常工作。

您需要設置SelectedSeason = AvailableSeasons[x]爲SelectedItem工作,因爲這使得兩個項目完全相同。或者您可以實施一些自定義方法來比較兩個季節,看看它們是否相同。通常我只是覆蓋正在比較的類的ToString()方法。