WPF DataGrid ComboBoxColumn 未插入值
我有一个带有组合框列和两个文本框列的 WPF 数据网格。在我的测试用例中,当加载屏幕时,网格绑定到的集合中有两行。如果我更改任何单元格的内容,它会正确更新。但是,如果我向网格添加新行,则当我更新组合框列中的值时,它不会在源集合中更新。不过,文本框列对于新添加的行可以正常工作。这些列的定义如下:
<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>
这是我的视图模型中的 PhoneNumbers 属性:
public ObservableCollection<PhoneNumber> PhoneNumbers
{
get
{
return _phoneNumbers;
}
set
{
if (value != _phoneNumbers)
{
_phoneNumbers = value;
OnPropertyChanged("PhoneNumbers");
}
}
}
更新:这是我的 PhoneNumber 类:
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.PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
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.PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,检查输出窗口中是否有任何绑定错误消息。
在我看来,当您添加新项目时,您的 Type 属性可能为 null。这实质上使得 Type 的 Id 属性不可访问。
尝试在 PhoneNumber 构造函数中实例化一个新的默认 Type 对象
,或者更好的是,将组合框直接绑定到类型而不是嵌套的 Type.Id(更改 SelectedValue Binding 并删除 SelectedValuePath。
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
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.