2012-03-19 39 views
0

在我的項目中,我將可觀察集合綁定到數據網格。每列DataGrid有一個組合框,它是綁定的屬性,並返回一個字符串列表。組合框的selecteditem綁定到可觀察集合的另一個屬性。 get訪問器在組合框中工作正常,但該集合不起作用。我需要這個工作因爲我必須將更改保存到我的數據庫中。爲什麼ComboBox selectedItem不會調用set訪問器

這是我的XAML

<DataGrid SelectedItem="{Binding SelectedPartProperty, Mode=TwoWay}" 
       ItemsSource="{Binding AllPartProperties}" AutoGenerateColumns="False" Grid.Row="1" Margin="416,6,302,0"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Name" Binding="{Binding PropertyName,Mode=TwoWay}" IsReadOnly="false"/> 
      <DataGridTemplateColumn Header="Data Type" IsReadOnly="false"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox SelectedItem="{Binding PropertyDataType, Mode=TwoWay}" 
          ItemsSource="{Binding DataType}" 
          Background="White" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 

     </DataGrid.Columns> 
    </DataGrid> 

我的主視圖模型的某些部分

public ObservableCollection<PartPropertyViewModel> AllPartProperties 
    { 
     get 
     { 
      if (SelectedPartNumber == null) 
      { 
       _allProperties = new ObservableCollection<PartPropertyViewModel>(); 
       return _allProperties; 
      } 
      PartPropertyViewModel tempPartPropertyViewModel; 
      ObservableCollection<PartPropertyViewModel> newPartPropertyViewModel = new ObservableCollection<PartPropertyViewModel>(); 
      foreach (PartProperty p in SelectedPartNumber.PartNumberEntity.PartProperties) 
      { 
       tempPartPropertyViewModel = new PartPropertyViewModel(p); 
       newPartPropertyViewModel.Add(tempPartPropertyViewModel); 
      } 

      _allProperties = newPartPropertyViewModel; 
      return _allProperties; 
     } 
     set 
     { 
      _allProperties = value;    

     } 

    } 

另一個視圖模型

class PartPropertyViewModel : ViewModelBase 
{ 
    private PartProperty _partPropertyEntity;  
    private string _propertyDataType; 
    private string[] _dataType; 
    private PartParameterViewModel _partParameters; 

    public PartPropertyViewModel(PartProperty partProperty) 
    { 
     PartPropertyEntity = partProperty; 
     _partParameters = new PartParameterViewModel(partProperty); 
    } 

    //public PartPropertyViewModel() 
    //{ 
    // //PartPropertyEntity = new PartProperty();   
    //} 


    public PartProperty PartPropertyEntity 
    { 
     get 
     { 
      return _partPropertyEntity; 
     } 
     set 
     { 
      _partPropertyEntity = value; 
     } 
    } 
    public string PropertyName 
    { 
     get 
     { 
      if (PartPropertyEntity == null || PartPropertyEntity.ConfigurationProperty == null) 
       return ""; 
      else 
       return PartPropertyEntity.ConfigurationProperty.chrCPProperty; 
     } 
     set 
     { 
      PartPropertyEntity.ConfigurationProperty.chrCPProperty = value; 
      OnPropertyChanged("PropertyName"); 
     } 
    } 

    public string[] DataType 
    { 
     get 
     { 
      _dataType = new string[] { "Number", "String"};     
      return _dataType; 
     } 
     set 
     { 
      _dataType = value; 
      OnPropertyChanged("DataType"); 
      OnPropertyChanged("PropertyDataType"); 
     } 

    } 
    public string PropertyDataType 
    { 
     get 
     { 
      if (PartPropertyEntity == null || PartPropertyEntity.ConfigurationProperty == null) 
       return ""; 

      if (PartPropertyEntity.ConfigurationProperty.bitCPIsNumeric) 
       _propertyDataType = DataType[0]; 
      else 
       _propertyDataType = DataType[1]; 
      return _propertyDataType; 
     } 
     set 
     { 
      _propertyDataType = value; 

      if (String.Compare(value,"Number") == 0) 
       PartPropertyEntity.ConfigurationProperty.bitCPIsNumeric = true; 
      else 
       PartPropertyEntity.ConfigurationProperty.bitCPIsNumeric = false; 

      OnPropertyChanged("PropertyDataType"); 
     } 
    } 



    public PartParameterViewModel PartParameters 
    { 
     get 
     { 
      return _partParameters; 
     } 
    } 

}//end of class 

回答

0

您綁定到一個字符串數組

public string[] DataType 

字符串沒有PropertyDataType屬性,我懷疑get也被調用。

需要綁定到像List或ObservableCollection這樣的集合

+0

Gotcha..will do that。 – IamaC 2012-03-19 15:21:09

相關問題