[英]Write in a ComboBoxColumn of DataGrid in WPF
I would like to have the possibility to write another value which is not in the list in a DatagridComboBoxColumn
我想写另一个不在DatagridComboBoxColumn
列表中的值的可能性
How could I do that? 我该怎么办?
In WinForm
I used to do it with 在WinForm
我曾经用
private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox c = e.Control as ComboBox;
if (c != null) c.DropDownStyle = ComboBoxStyle.DropDown;
}
private void DataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == dataGridViewTextBox.Index)
{
object eFV = e.FormattedValue;
if (!dataGridViewTextBox.Items.Contains(eFV))
{
DataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = eFV;
}
}
}
It is possible to use TargetNullValue
and FallbackValue
: 可以使用TargetNullValue
和FallbackValue
:
<DataGrid>
<DataGridComboBoxColumn ItemsSource="{Binding EmployeeNames,
TargetNullValue='Oops, I am missing',FallbackValue='I am default'}"/>
</DataGrid>
As MSDN says: 正如MSDN所说:
FallbackValue - Gets or sets the value to use when the binding is unable to return a value. FallbackValue-获取或设置绑定无法返回值时要使用的值。 TargetNullValue - Gets Gets or sets the value that is used in the target when the value of the source is null. TargetNullValue -Gets获取或设置当源的值为null时在目标中使用的值。
Update: 更新:
ViewModel: ViewModel:
private ObservableCollection<Person> persons;
public ObservableCollection<Person> Persons
{
get { return persons; }
set { persons = value;
OnPropertyChanged("Persons");
}
}
private void FillData()
{
persons = new ObservableCollection<Person>();
for (int i = 0; i < 30; i++)
{
if(i%2==0)
persons.Add(new Person() { IdPerson=i, Name="", SurName="Albahari"});
else
persons.Add(new Person() { IdPerson = i, Name = "Ben & Joseph " + i.ToString(), SurName = "Albahari" });
}
//If you want set some value, you can do it:
if(persons[0].Name=="")
persons[0].Name = "That should be Name";
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.