开发者

WPF DataGrid ComboBoxColumn not inserting value

I have a WPF datagrid with a combobox column and two textbox columns. In my test case, when the screen is loaded, there are two rows in the collection to which the grid is bound. If I change the contents of any of the cells, it updates properly. However, if I add a new row to the grid, when I update the value in the combobox column, it is not updated in the source collection. The textbox columns work properly for newly added rows though. The columns are defined as such:

<DataGrid.Columns>
    <DataGridComboBoxColumn Header="Type" Width="*" SelectedValueBinding="{Binding Mode=TwoWay, Path=Type.Id}"
       ItemsSource="{Binding Source={StaticResource PhoneTypeList}, Path=PhoneTypes}"
       SelectedValuePath="Id" DisplayMemberPath="Type" />
    <DataGridTextColumn Binding="{Binding NotifyOnTargetUpdated=True, Path=Number, Mode=TwoWay, ValidatesOnExceptions=False}" Header="Number" Width="*"/>
    <DataGridTextColumn Binding="{Binding NotifyOnSourceUpdated=True, Path=Extension, ValidatesOnExceptions=False}" Header="Extension" Width="*"/>
</DataGrid.Columns>

Here is the PhoneNumbers property in my viewmodel:

public ObservableCollection<PhoneNumber> PhoneNumbers
        {
            get
            {
                return _phoneNumbers;
            }
            set
            {
                if (value != _phoneNumbers)
                {
                    _phoneNumbers = value;
                    OnPropertyChanged("PhoneNumbers");
                }
            }
        }

Update: Here is my PhoneNumber class:

public class PhoneNumber : INotifyPropertyChanged
    {
        private string _number;
        private string _extension;
        private PhoneType _type;

        public PhoneType Type { get { return _type; }
            set { _type = value; OnPropertyChanged("Type"); } }
        public string Number
        {
            set
            {
                _number = value;
                OnPropertyChanged("Number");
            }
            get { return _number; }
        }

        public string Extension
        {
            set
            {
                _extension = value;
                OnPropertyChanged("Extension");
            }
            get { return _extension; }
        }

        public override string ToString()
        {
            return Number + (!string.IsNullOrEmpty(Extension) ? " x " + Extension : "");
        }

        public event System.ComponentModel.PropertyChangedEven开发者_运维百科tHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this,
                    new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }


First, check your output window for any binding error messages.

It looks to me like you Type property might be null when you add a new item. This essentially makes the Id property of Type inaccessible.

Try instantiating a new default Type object in your PhoneNumber Constructor

PhoneNumber() 
{
    _type = new PhoneType();
}

Or better yet, bind your combo box directly to the type instead of to the nested Type.Id (change SelectedValue Binding and remove the SelectedValuePath.

<DataGridComboBoxColumn Header="Type" Width="*" SelectedValueBinding="{Binding Mode=TwoWay, Path=Type}"
   ItemsSource="{Binding Source={StaticResource PhoneTypeList}, Path=PhoneTypes}"
   DisplayMemberPath="Type" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜