为什么ComboBox selectedItem不调用set访问器

时间:2012-03-19 14:23:41

标签: wpf data-binding mvvm combobox

在我的项目中,我将一个可观察的集合绑定到一个数据网格。每一列数据网格都有一个组合框,它是绑定属性并返回一个字符串列表。组合框的selecteditem绑定到我的可观察集合的另一个属性。 get访问器在组合框中工作正常但该集合不起作用。我需要这个工作因为我必须将更改保存到我的数据库。

以下是我的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;              

        }

    }

另一个viewModel

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

1 个答案:

答案 0 :(得分:0)

您绑定到字符串数组

  public string[] DataType 

String没有PropertyDataType属性,我怀疑是否调用了get。

需要绑定到List或ObservableCollection等集合