2016-03-14 90 views
0

我迷路了...... 我檢查的一切,我覺得coud的..WPF UI更新不及時

  1. DataContext設置!
  2. 在DebugOutputWindow沒有BindingErrors
  3. INotifyPropertyChanged的正確實施(因爲鍵式操作的工作...)
  4. 集合更新(計數> 0)

但不會顯示在我的ComboBox中的項目。

這裏還有什麼可以錯的?

這裏是我的代碼:

private void Refresh(bool isAuto) 
{ 
     List<DatabaseEntry> entries = GetBrokenJobs(isAuto); 

     Collection.Clear(); 
     foreach (string jobName in entries.Select(x => x.Description).ToList()) 
     { 
      Collection.Add(jobName); 
     } 
} 

我綁定到一個ComboxBox:

ItemsSource="{Binding Path=Collection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 

private ObservableCollection<string> _collection = new ObservableCollection<string>(); 
public ObservableCollection<string> Collection 
{ 
    get {return _collection; } 
    set 
    { 
     _collection = value; 
     OnPropertyChanged();//<= Has [CallerMemberName] in constructor ... 
    } 
} 

而且我設置的DataContext像往常一樣..

this.DataContext = MainViewModel.Instance; 

因爲我所有的按鈕和複選框工作,這可能不是原產地..

編輯

下面是一些更多的XAML

<ComboBox x:Name="ComboBox" 
         Grid.Row="1" 
         Margin="10"       
         Grid.Column="1" 
         Width="230" 
         VerticalContentAlignment="Center" 
         HorizontalContentAlignment="Center" 
         IsEnabled="{Binding Path=IsJobSelectorEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
         SelectedItem="{Binding Path=SelectedJobItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
         ItemsSource="{Binding Path=Collection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
      </ComboBox> 
+4

請使用Snoop在運行時檢查您的DataContext和綁定。並請刪除模式= TowWay和UpdateSourceTrigger - 它是沒有意義的綁定到ItemsSource – blindmeis

+0

Weired的事情是:我有CheckBoxes,他們的工作,與命令在同一視圖模型上的按鈕也做。 DataContext設置爲100%... –

+3

無法使用xaml – Muds

回答

1

解決方法很簡單。將組合框項目源綁定到ObservableCollection。

這不是必需的 的ItemsSource = 「{綁定路徑=收集,模式=雙向,UpdateSourceTrigger =的PropertyChanged}」

請使用類似這樣的 的ItemsSource = 「{結合ComboBoxList}」 哪裏ComboBoxList是在數據上下文中定義的可觀察集合。

0

我知道它有點違背什麼observablecollection應該做的,但我有一個問題,在過去添加到現有的ObservableCollection被完全忽略。

如果您嘗試更換整個的ObservableCollection這樣的東西線沿線的,它可能會迫使它更新:

列表條目= GetBrokenJobs(isAuto);

Collection = new ObservableCollection(entries.Select(x => x.Description).ToList());