2016-09-13 37 views
0

我有2 ComboBoxes的視圖。當第一個項目被選中時,第二個項目將被列出。鏈接視圖和視圖模型的屬性如下:組合框單擊/打開時會停止更新

在代碼I連接的視圖和視圖模型

var viewModel = new MetrologyFileViewModel(); 
DataContext = viewModel; 

WPF視圖的背後:

<ComboBox ItemsSource="{Binding MetrologyProperties}" 
      SelectedIndex="{Binding NewSelectedMetrologyPropertyIndex}" 
      Grid.Column="1" 
      Grid.ColumnSpan="2" 
      Style="{StaticResource ComboBoxStyle1}" /> 
<ComboBox ItemsSource="{Binding NewMetrologyData}" 
      SelectedIndex="{Binding NewSelectedMetrologyDataIndex}" 
      Grid.Row="1" 
      Grid.Column="1" 
      Grid.ColumnSpan="2" 
      Style="{StaticResource ComboBoxStyle1}" /> 

C#:

/// <summary> 
/// The new metrology property of the metrology data. 
/// </summary> 
public int NewSelectedMetrologyPropertyIndex 
{ 
    get { return _newSelectedMetrologyPropertyIndex; } 
    set 
    { 
     _newSelectedMetrologyPropertyIndex = value; 

     _newMetrologyData = _newSelectedMetrologyPropertyIndex > 0 ? new ObservableCollection<MetrologyData> { new MetrologyData() }.AddRange(DbServiceMetrologyData.GetOnProperty(MetrologyProperties[_newSelectedMetrologyPropertyIndex])) : null; 

     NotifyPropertyChanged(); 
     NotifyPropertyChanged(nameof(NewMetrologyData)); 
    } 
} 

/// <summary> 
/// The new available metrology data that can be chosen from. 
/// </summary> 
public ObservableCollection<MetrologyData> NewMetrologyData 
{ 
    get { return _newMetrologyData; } 
    set 
    { 
     _newMetrologyData = value; 
     NotifyPropertyChanged(); 
    } 
} 

/// <summary> 
/// The new metrology data of the metrology fie. 
/// </summary> 
public int NewSelectedMetrologyDataIndex 
{ 
    get { return _newSelectedMetrologyDataIndex; } 
    set 
    { 
     _newSelectedMetrologyDataIndex = value; 
     NotifyPropertyChanged(); 
    } 
} 

AddRange方法:

/// <summary> 
/// Add a range of items to a <see cref="ObservableCollection{T}"/>. 
/// </summary> 
/// <typeparam name="T">The type of the <see cref="ObservableCollection{T}"/></typeparam> 
/// <param name="sourceCollection">The <see cref="ObservableCollection{T}"/> where the items have to be added to.</param> 
/// <param name="newCollection"><see cref="ObservableCollection{T}"/> with the new items.</param> 
    public static ObservableCollection<T> AddRange<T>(this ObservableCollection<T> sourceCollection, ObservableCollection<T> newCollection) 
{ 
    if (newCollection != null) 
     foreach (var item in newCollection) 
      sourceCollection.Add(item); 

    return sourceCollection; 
} 

我第一次選擇在第一ComboBox一個屬性,因爲應顯示列表中的空間非常小(見圖片:第二個更新罰款雖然觀點(我認爲)沒有完全更新)。

只要我不點擊第二個ComboBox,它就會繼續工作。從我打開(點擊)第二個ComboBox的那一刻起,當我在第一個屬性中選擇一個屬性時,它停止更新。

enter image description here

enter image description here


更新 我發現只調用DbServiceMetrologyData.GetOnProperty(MetrologyProperties[_newSelectedMetrologyPropertyIndex])在解決問題的時候,我不知道爲什麼,雖然。

+0

如果你已經找到了答案,那麼請關閉你的問題,然後 – Versatile

+0

把邏輯放在MVVM屬性設置器中是不好的做法,你應該爲組合框實現SelectionChangedCommand處理器,並在視圖模型中處理它。 –

回答

0

我發現,只是打電話DbServiceMetrologyData.GetOnProperty(MetrologyProperties[_newSelectedMetrologyPropertyIndex])問題已修復,但我不知道爲什麼。

相關問題