我希望有可能写一个不在DatagridComboBoxColumn
我怎么能这样做?
在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;
}
}
}
答案 0 :(得分:1)
可以使用TargetNullValue
和FallbackValue
:
<DataGrid>
<DataGridComboBoxColumn ItemsSource="{Binding EmployeeNames,
TargetNullValue='Oops, I am missing',FallbackValue='I am default'}"/>
</DataGrid>
正如MSDN所说:
FallbackValue - 获取或设置绑定时要使用的值 无法返回值。 TargetNullValue - 获取或设置 当源的值为null时,在目标中使用的值。
<强>更新强>
视图模型:
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";
}